Native HTTP/2 support in frameworks
Node recently launched native support for HTTP/2 in their core. In a previous post, we covered why this is an important step and how you can start building basic servers using HTTP/2 already.
However, a lot of developers use MVC or full-stack Node frameworks such as Hapi and Express. Thus, support of HTTP/2 by popular Node frameworks is crucial to ensure adoption. We are monitoring the status of support as most frameworks are actively working on it.
In this post, we will track the HTTP/2 support status in some of the most popular Node frameworks and provide examples of how to implement basic HTTP/2 servers where possible.
For HTTP/2 to work, you would need HTTPS enabled. If you do not have it already, you can generate a free Let’s Encrypt certificate or a self-signed certificate for your own local server to get started.
Hapi {#hapi}
Hapi (from v16.6.2) supports HTTP/2 natively.
You can install Hapi with the following command:
npm install hapi
Building a HTTP/2 server with Hapi is pretty straightforward:
var fs = require('fs');
var Hapi = require('hapi');
var http2 = require('http2');
var options = {
key: fs.readFileSync('./selfsigned.key'),
cert: fs.readFileSync('./selfsigned.crt'),
};
var server = new Hapi.Server();
server.connection({
listener: http2.createSecureServer(options),
host: 'localhost',
port: 443,
tls: true
});
server.route({
method: 'GET',
path:'/hello',
handler: function (request, reply) {
return reply('hello world');
}
});
server.start(err => {
if (err) console.error(err)
console.log(`Started ${server.connections.length} connections`)
})
Koa {#koa}
Koa (at least from v2.3.0 onwards) supports HTTP/2 out of the box too.
Start with installing Koa:
npm install koa
We then setup a simple server over HTTP/2:
const fs = require('fs');
const http2 = require('http2');
const koa = require('koa');
const options = {
key: fs.readFileSync('./selfsigned.key'),
cert: fs.readFileSync('./selfsigned.crt'),
};
const app = new koa();
// response
app.use(ctx => {
ctx.body = 'Hello Koa';
});
const server = http2.createSecureServer(options, app.callback());
server.listen(443);
Express {#express}
Express doesn’t support HTTP/2 yet, but the developers are actively working on it. We will update the post once we find a working solution.
We are following this issue to stay up to date when Express launches HTTP/2 support.
Note that Express does seem to work with the SPDY module. However, SPDY is now dead.
Meteor {#meteor}
HTTP/2 is the second-most requested Meteor feature! We are closely following the progress and will update this post.
Sails {#sails}
Since Sails is dependent on Express, we expect HTTP/2 support of Sails will follow soon after that of Express. You can follow the progress on their roadmap here and discussion on their Github issue here.
Conclusion
Did you find any other Node framework supporting HTTP/2 now? Let us know in the comments below!
It is undoubtedly important for the front-end community to go performance-first. We are tracking how latest web technologies are adopted in the most popular development tools.