In this tutorial we will see how to get HTTP headers from a URL using curl and Node.js.

The curl command must be executed from the shell and its output must be transformed into a literal object having as properties and values respectively the name and the value of each HTTP header.

We can define the following class:

'use strict';

const util = require('util');
const exec = util.promisify(require('child_process').exec);
const validator = require('validator');

class cURL {
    constructor(url) {
        this.url = url;
    }

    validate() {
        return typeof this.url === 'string' && validator.isURL(this.url);
    }

    parseHeaders(output) {
        if(!output || output.length === 0) {
            return null;
        }
        const lines = output.split(/\r\n/);
        let headers = {};
        lines.forEach(line => {
           let values = line.split(/:\s/);
           if(values.length === 2 && typeof values[0] === 'string' && typeof values[1] === 'string') {
               let [key, value] = values;
               headers[key] = value.trim();
           }
        });
        return headers;
    }

    async getHeaders() {
        if(!this.validate()) {
            throw new Error('Invalid parameter.');
        }
        const cmd = `curl -I ${this.url}`;

        try {
            const { stdout, stderr } = await exec(cmd);
            return this.parseHeaders(stdout);
        } catch(err) {
            return err;
        }
    }
}

module.exports = cURL;

The command to run from the shell is curl -I <URL>. The URL must be validated and if it passes this validation it can be used with the core exec() method which in this example returns a Promise using the promisify() utility method.

The command output is formatted on multiple lines. In each line the header name is separated from its value by the pattern :[space], so we can use this pattern to separate lines and headers and to create the literal object returned by the main method of the class.

Example of use:

'use strict';

const curl = require('./lib/cURL');
const curlRequest = new curl('https://gabrieleromanato.com');

(async() => {
    try {
        console.log(await curlRequest.getHeaders());
    } catch(err) {
        console.log(err);
    }
})();