Node.js: making HTTP GET requests with the core modules

Node.js: making HTTP GET requests with the core modules

In this article we will see how to make GET requests using Node.js core modules.

In this article we will see how to make GET requests using Node.js core modules.

The first step is to parse the URL passed as a parameter to our main function or method. We can use the JavaScript class URL for this purpose.

'use strict';

const parseURL = url => {
    try {
        return new URL(url);
    } catch(err) {
        return null;
    }    
};

module.exports = parseURL;

If we read the documentation we will notice that instantiating this class may raise a TypeError exception if the URL passed to the constructor is invalid, then we need to handle that possible exception.

If the URL is valid, we now have an object containing three fundamental properties for the purpose we want to achieve, namely:

  1. protocol: the protocol used, essential for choosing between the core modules that manage the HTTP and HTTPS protocols.
  2. hostname: the name of the domain we want to make the request to.
  3. pathname: the relative path on the server, for example /posts/post-title.

At this point we can define our main function or method.

'use strict';

const parseURL = require('./helpers/parseURL');

const request = url => {
    return new Promise((resolve, reject) => {
        const urlObj = parseURL(url);
        if(urlObj === null) {
            reject({ error: 'Invalid URL' });
        }
        const { protocol, hostname, pathname } = urlObj;
        if(protocol !== 'http:' || protocol !== 'https:') {
            reject({ error: 'Invalid protocol' });
        }
        const moduleProtocol = protocol.replace(':', '');
        const module = require(moduleProtocol);
        const port = moduleProtocol === 'https' ? 443 : 80;
        const options = {
            hostname: hostname,
            port: port,
            path: pathname,
            method: 'GET'
            };
            const req = module.request(options, res => {
            let body = '';
            res.on('data', d => {
                body += d;
            });
            res.on('end', () => { return resolve({body}); });
            });
            
            req.on('error', error =>  { return reject({ error }); });

            req.end();
    });
};

If the URL is valid, we extract the relevant components and move to the identification of the protocol, which must necessarily be either HTTP or HTTPS. We then remove the : character from the protocol to include either the core http module for the HTTP protocol or the core https module for the HTTPS protocol. Depending on the protocol in use, we will choose port 80 for the HTTP protocol or port 443 for the HTTPS protocol.

The whole logic is wrapped inside a Promise, so we will be able to use the async/await model to handle GET requests in the future.