Menu

HTTP/2 in Node.js core

November 13th, 2017

  • If you recall our last blogposts on HTTP/2, such as HTTP/2: a look into the future of the web, Alternatives to HTTP/2 or Optimize with Server Push and Service Workers, you may remember some of the details in the HTTP/2 protocol and the differences from version one.
  • Check out the following example: – – const http2 = require(‘http2’), – fs = require(‘fs’); – – const options = { – key: cert: server = – server.on(‘stream’, (stream, requestHeaders) = { – stream.respond({ – ‘content-type’: ‘text/html’, – ‘:status’: 200 – }); –  stream.end(‘h1Hi, and welcome to YLD – -…
  • The server you are talking with supports ${alpnProtocol} `); – } – – const server = http2.createSecureServer( – options, – and response handlers will work as expected, despite the fact they already have HTTP/2 extra information such as if the connection is using ALPN (used in HTTP/2 only).
  • In the next example we can see that, every time the index route is requested, a CSS file is pushed: – – const onRequestHandler = (req, res) = { – const currentUrl = url.parse(req.url); – – if (currentUrl.pathname === ‘/’) { – const cssFile = { – path: ‘/style.css’, -…
  • /style.css’, – headers: { – ‘content-type’: ‘text/css’ – } – }; –    pushAsset(res.stream, cssFile); – – // … – – – and you could implement pushAsset as follows: – – const pushAsset = (stream, file) = { – const filePath = path.join(__dirname, file.filePath); – stream.pushStream({ [HTTP2_HEADER_PATH]: file.path }, (pushStream)…

HTTP/2 is starting to be used more and more (it jumped from 11% in the beginning of this year to 18% total usage on the web). If you recall our last blogposts on HTTP/2, such as HTTP/2: a look into the future of the web, Alternatives to HTTP/2 or Optimize with Server Push and Service Workers, you may remember some of the details in the HTTP/2 protocol and the differences from version one. Check them out if you have not, as we are not going into RFC details in this blogpost.

HTTP/2 is starting to be used more and more (it jumped from 11% in the beginning of this year to 18% total usage on the web). If you recall our last blogposts on HTTP/2, such as HTTP/2: a look into the future of the web, Alternatives to HTTP/2 or Optimize with Server Push and Service Workers, you may remember some of the details in the HTTP/2 protocol and the differences from version one. Check them out if you have not, as we are not going into RFC details in this blogpost.

In our latest blogposts, we mentioned that a version of HTTP/2 was starting to be implemented under the nodejs/http2 repository, but it was not decided at the time if it was going to be merged into the Node.js core modules or if it would be an external module. The main reason behind having a new implementation was that other implementations do not fully respect the HTTP/2 RFC.

Eventually, a pull request was submitted to add it to core. It was merged and included as experimental under a flag (–expose-http2) in August (version 8.4.0).

After the Twitter announcement saying that the flag was about to be removed in 8.x LTS release, Node.js eventually got rid of the flag in version 8.8.0 (Oct, 24). HTTP/2 is still considered to be experimental and despite the fact it is still not 100% finished, you can already start experimenting…

HTTP/2 in Node.js core