Search code examples
node.jshttp2

Performing outgoing HTTP2 requests in NodeJS


I've checked the NodeJS documentation but could not find any information on how to make the following code use HTTP2 to carry out the request:

const https = require('https');

const options = {
  hostname: 'encrypted.google.com',
  port: 443,
  path: '/',
  method: 'GET'
};

const req = https.request(options, (res) => {
  console.log('statusCode:', res.statusCode);
  console.log('headers:', res.headers);

  res.on('data', (d) => {
    process.stdout.write(d);
  });
});

req.on('error', (e) => {
  console.error(e);
});

req.end()

Is this simply not supported yet by even the most recent versions of NodeJS?


Solution

  • This is available in v9.9.0. You can take a look at HTTP2 in Nodejs. You can create a secureServer in HTTP2 if you want the whole thing. Else firing off requests using http2 is available too. You can take a look at this article for some ideas