Documentation

HTTP2

Fastify offers experimental support for HTTP2 starting from Node 8.8.0, which includes HTTP2 without a flag. Fastify supports HTTP2 both over HTTPS or over plaintext.

Currently none of the HTTP2-specific APIs are available through Fastify, but Node's req and res can be access through our Request and Reply interface. PRs are welcome.

Secure (HTTPS)

HTTP2 is supported in all modern browsers only over a secure connection:

'use strict'

const fastify = require('fastify')({
  http2: true,
  https: {
    key: fs.readFileSync(path.join(__dirname, '..', 'https', 'fastify.key')),
    cert: fs.readFileSync(path.join(__dirname, '..', 'https', 'fastify.cert'))
  }
})

fastify.get('/', function (req, reply) {
  reply.code(200).send({ hello: 'world' })
})

fastify.listen(3000)

ALPN negotiation allows to support both HTTPS and HTTP/2 over the same socket. Node core req and res objects can be either HTTP/1 or HTTP/2. Fastify supports this out of the box:

'use strict'

const fastify = require('fastify')({
  http2: true,
  https: {
    allowHTTP1: true, // fallback support for HTTP1
    key: fs.readFileSync(path.join(__dirname, '..', 'https', 'fastify.key')),
    cert: fs.readFileSync(path.join(__dirname, '..', 'https', 'fastify.cert'))
  }
})

// this route can be accessed through both protocols
fastify.get('/', function (req, reply) {
  reply.code(200).send({ hello: 'world' })
})

fastify.listen(3000)

You can test your new server with:

$ npx h2url https://localhost:3000

Plain or insecure

If you are building microservices, you can connect to HTTP2 in plain text, however this is not supported by the browers.

'use strict'

const fastify = require('fastify')({
  http2: true
})

fastify.get('/', function (req, reply) {
  reply.code(200).send({ hello: 'world' })
})

fastify.listen(3000)

You can test your new server with:

$ npx h2url http://localhost:3000