1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/mirrors-Fastify

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
Клонировать/Скачать
HTTP2.md 2.1 КБ
Копировать Редактировать Web IDE Исходные данные Просмотреть построчно История
Frazer Smith Отправлено 19.01.2025 16:57 0b00f7b

Fastify

HTTP2

Fastify supports HTTP2 over HTTPS (h2) or plaintext (h2c).

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

Secure (HTTPS)

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

'use strict'

const fs = require('node:fs')
const path = require('node:path')
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 (request, reply) {
  reply.code(200).send({ hello: 'world' })
})

fastify.listen({ port: 3000 })

ALPN negotiation allows support for 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 fs = require('node:fs')
const path = require('node:path')
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 (request, reply) {
  reply.code(200).send({ hello: 'world' })
})

fastify.listen({ port: 3000 })

Test the new server with:

$ npx h2url https://localhost:3000

Plain or insecure

For microservices, HTTP2 can connect in plain text, but this is not supported by browsers.

'use strict'

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

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

fastify.listen({ port: 3000 })

Test the new server with:

$ npx h2url http://localhost:3000

Опубликовать ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://api.gitlife.ru/oschina-mirror/mirrors-Fastify.git
git@api.gitlife.ru:oschina-mirror/mirrors-Fastify.git
oschina-mirror
mirrors-Fastify
mirrors-Fastify
main