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

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...

A Beginner’S Guide To Adobe Analytics

Benefits of Learning Adobe Analytics Similar to tools like Google Analytics 360,  Adobe Analytics   offers notable benefits and features (avoiding sampling, custom tracking flexibility, etc.), justifying the notable yearly cost associated to it. Becoming familiar with Adobe will naturally expand your analytics skills! While many elements of Google Analytics tracking are applicable to Adobe; Adobe’s new features, tools, and caveats will deepen and enrich your analytics knowledge base. If you understand the features within Adobe, you will   save yourself time as well as others by quickly solving complex analysis problems, creating expansive and actionable reporting, and setting up strategic tracking. Comparing Dimension and Metric Set-Ups in GA and Adobe Now that we’ve established why Adobe Analytics is important, let’s look at some high-level dimension and metrics differences between Google Analytics and Adobe Anal...