Skip to main content

How to get node.js HTTP request promise without a single dependency

Sometimes one needs just to read a body of simple HTTP(S) GET response, without any complicated logic and dozens of NPM dependencies involved. So why not to use all the goodies node.js core provides us.

const getContent = function(url) {
  // return new pending promise
  return new Promise((resolve, reject) => {
    // select http or https module, depending on reqested url
    const lib = url.startsWith('https') ? require('https') : require('http');
    const request = lib.get(url, (response) => {
      // handle http errors
      if (response.statusCode < 200 || response.statusCode > 299) {
         reject(new Error('Failed to load page, status code: ' + response.statusCode));
       }
      // temporary data holder
      const body = [];
      // on every content chunk, push it to the data array
      response.on('data', (chunk) => body.push(chunk));
      // we are done, resolve promise with those joined chunks
      response.on('end', () => resolve(body.join('')));
    });
    // handle connection errors of the request
    request.on('error', (err) => reject(err))
    })
};
There is not a single external dependency included. Usage is then rather simple, due to Promise interface:
getContent('https://www.random.org/integers/?num=1&min=1&max=100&col=1&base=10&format=plain&rnd=new')
  .then((html) => console.log(html))
  .catch((err) => console.error(err));
Sure, we have no body parsing, no JSON validation, no encoding conversion. But do you need it anyway?
The typical recommended solution includes frequently request package. Have you ever seen the dependencies tree of this?
There were 32 new releases of request package in 2015. Are you ready to update your project every time one of those dependencies discovers a security vulnerability and forces the whole tree to release new versions? Or would you rather use the standard library for such a simple task?
Of course, if you have much more complicated requirements, dozens of dependencies already included, maybe you should just add request-promise and let it do its job:
var rp = require('request-promise');
rp('http://www.google.com')
    .then((html) => console.log(html)) // Process html...
    .catch((err) => console.error(err)); // Crawling failed...
But before you do that, think how to keep things simple.

Comments

Popular posts from this blog

Better Tableau implementation gives BI dashboards a boost

When the telecommunications group at General Motors initially began using Tableau dashboards to track service requests and incidents, reports had such long load times that managers couldn't use them effectively. "It really wasn't something they could use on their own, let alone use in a meeting," said Katrina Botting, who heads data strategy and intelligence for GM's telecom organization, in a presentation at Tableau Conference 2017. It wasn't until last year, when Botting and her team learned how to effectively utilize the software's data extract feature as part of GM's Tableau implementation, that reports loaded quickly enough to be truly self-service. The extract feature loads a snapshot of data stored on disk into memory; now, virtually all reports are based on extracted data, and reports that once took several minutes to load do so in seconds, Botting said. Managers in the telecom unit used to meet with each other just to make sure they a...

A short Introduction to Human capital management (HCM)

Human capital management ( HCM ) is the comprehensive set of practices for recruiting, managing, developing and optimizing the human resources of an organization. The phrase connotes an approach to human resource management (HRM) that views employees as assets that can be invested in and managed to maximize their business value. Software HCM has come to be nearly synonymous with the human resources (HR) function in organizations. In HR technology, the comprehensive software systems for managing HR processes differ little from HCM suites. For example, the functions of most human resource information systems (HRIS) are often the same as HCM systems. However, some observers use HCM in a narrow sense to denote just the labor-scheduling and time-tracking functions of HR. HCM suites are sold either as components of enterprise resource planning (ERP) systems or as separate products that are typically integrated with ERP. In recent years, on-premises HCM has been superseded by software as ...

The keys to a successful business intelligence strategy

Business intelligence (BI) is essential for business growth and competitive advantage, yet reaping benefits from BI requires more than implementing the technology that enables it. In fact, deploying the technology is the easiest part of any BI initiative, according to Boris Evelson, vice president and principal analyst at Forrester Research. Getting the personnel and processes portions right are much more challenging, he says. As such, organisations must addresses personnel and processes as key facets of their  BI strategy  if they want to be successful. Moreover, BI strategies should be broken down even further to address ownership and continual improvement as well. These are the seven essential components of any successful BI strategy, according to BI experts. Give business ownership over BI Organisations that place BI in the hands of business users have greater success rates than those who confine BI within IT, Evelson says. This may mean embedding BI within line...