nodejs HTTP request without any dependencies

Sometimes it's useful not to use third party code if the implementation is simple enough.

const https = require('https');
const http = require('http');
const { URL } = require('url');

function request(url) {
  const parsedUrl = new URL(url);

  return new Promise((resolve, reject) => {
    const _protocol = parsedUrl.protocol === 'http' ? http : https;

    _protocol.request({
      host: parsedUrl.host,
      path: parsedUrl.pathname
    }, function(response) {

      let str = '';

      response.on('data', function (chunk) {
        str += chunk;
      });

      response.on('end', function () {
        resolve(str);
      });
    }).end();
  });
}

request('www.someurl.com/foo-bar-test').then(data => {
  console.log(data);
});