strong-tie/inbound-calls
0
1<h1 align="center">Fastify</h1>2 3## HTTP24 5_Fastify_ supports HTTP2 over either HTTPS (h2) or plaintext (h2c).6 7Currently, none of the HTTP2-specific APIs are available through _Fastify_, but8Node's `req` and `res` can be accessed through our `Request` and `Reply`9interface. PRs are welcome.10 11### Secure (HTTPS)12 13HTTP2 is supported in all modern browsers __only over a secure connection__:14 15```js16'use strict'17 18const fs = require('node:fs')19const path = require('node:path')20const fastify = require('fastify')({21 http2: true,22 https: {23 key: fs.readFileSync(path.join(__dirname, '..', 'https', 'fastify.key')),24 cert: fs.readFileSync(path.join(__dirname, '..', 'https', 'fastify.cert'))25 }26})27 28fastify.get('/', function (request, reply) {29 reply.code(200).send({ hello: 'world' })30})31 32fastify.listen({ port: 3000 })33```34 35[ALPN negotiation](https://datatracker.ietf.org/doc/html/rfc7301) allows36support for both HTTPS and HTTP/2 over the same socket.37Node core `req` and `res` objects can be either38[HTTP/1](https://nodejs.org/api/http.html) or39[HTTP/2](https://nodejs.org/api/http2.html). _Fastify_ supports this out of the40box:41 42```js43'use strict'44 45const fs = require('node:fs')46const path = require('node:path')47const fastify = require('fastify')({48 http2: true,49 https: {50 allowHTTP1: true, // fallback support for HTTP151 key: fs.readFileSync(path.join(__dirname, '..', 'https', 'fastify.key')),52 cert: fs.readFileSync(path.join(__dirname, '..', 'https', 'fastify.cert'))53 }54})55 56// this route can be accessed through both protocols57fastify.get('/', function (request, reply) {58 reply.code(200).send({ hello: 'world' })59})60 61fastify.listen({ port: 3000 })62```63 64You can test your new server with:65 66```67$ npx h2url https://localhost:300068```69 70### Plain or insecure71 72If you are building microservices, you can connect to HTTP2 in plain text,73however, this is not supported by browsers.74 75```js76'use strict'77 78const fastify = require('fastify')({79 http2: true80})81 82fastify.get('/', function (request, reply) {83 reply.code(200).send({ hello: 'world' })84})85 86fastify.listen({ port: 3000 })87```88 89You can test your new server with:90 91```92$ npx h2url http://localhost:300093```94 95 