如何将 HTTP/2 与 Nest.js 一起使用(节点)
How to use HTTP/2 with Nest.js (Node)
我读到 Express 4.x 与 Node.js 本机 HTTP2(来自 8.4+)不兼容,我希望 Express 5.x 比它有更多的进步。
但是当我开始考虑我的下一个 Node.js 项目时,Express5.x 可能会延迟发布 - 我过来了 Nest.js.
有谁知道 Nest.js 是否可以与本机 HTTP2 支持一起使用??
我听说的唯一支持此功能的 Node.js 框架是 Fastify。
或者还有其他的吗?支持Express插件的最好。
正如 Barry Pollard 评论的那样;为静态资源和 Webapp 本身使用前面的网络服务器,并为 API 目的使用 Node.js,无论如何可能是最好的方法。
您可以使用 node-spdy 包在 NestJS 中使用 HTTP/2(和 SPDY):
安装包
yarn add spdy
yarn add -D @types/spdy
生成证书
H2一般需要TLS,所以生成新的密钥和证书:
openssl req -x509 -newkey rsa:2048 -nodes -sha256 -keyout test.key -out test.crt
修改启动
接下来,修改main.ts
:
// main.ts
async function bootstrap() {
const expressApp: Express = express();
const spdyOpts: ServerOptions = {
key: fs.readFileSync('./test.key'),
cert: fs.readFileSync('./test.crt'),
};
const server: Server = spdy.createServer(spdyOpts, expressApp);
const app: NestApplication = await NestFactory.create(
AppModule,
new ExpressAdapter(expressApp),
);
await app.init();
await server.listen(3000);
}
bootstrap();
测试客户端
$ curl -I -k https://localhost:3000/
HTTP/2 200
x-powered-by: Express
content-type: text/html; charset=utf-8
content-length: 12
etag: W/"c-Lve95gjOVATpfV8EL5X4nxwjKHE"
在响应 headers 中发送通知 HTTP/2
。
我读到 Express 4.x 与 Node.js 本机 HTTP2(来自 8.4+)不兼容,我希望 Express 5.x 比它有更多的进步。 但是当我开始考虑我的下一个 Node.js 项目时,Express5.x 可能会延迟发布 - 我过来了 Nest.js.
有谁知道 Nest.js 是否可以与本机 HTTP2 支持一起使用??
我听说的唯一支持此功能的 Node.js 框架是 Fastify。 或者还有其他的吗?支持Express插件的最好。
正如 Barry Pollard 评论的那样;为静态资源和 Webapp 本身使用前面的网络服务器,并为 API 目的使用 Node.js,无论如何可能是最好的方法。
您可以使用 node-spdy 包在 NestJS 中使用 HTTP/2(和 SPDY):
安装包
yarn add spdy
yarn add -D @types/spdy
生成证书
H2一般需要TLS,所以生成新的密钥和证书:
openssl req -x509 -newkey rsa:2048 -nodes -sha256 -keyout test.key -out test.crt
修改启动
接下来,修改main.ts
:
// main.ts
async function bootstrap() {
const expressApp: Express = express();
const spdyOpts: ServerOptions = {
key: fs.readFileSync('./test.key'),
cert: fs.readFileSync('./test.crt'),
};
const server: Server = spdy.createServer(spdyOpts, expressApp);
const app: NestApplication = await NestFactory.create(
AppModule,
new ExpressAdapter(expressApp),
);
await app.init();
await server.listen(3000);
}
bootstrap();
测试客户端
$ curl -I -k https://localhost:3000/
HTTP/2 200
x-powered-by: Express
content-type: text/html; charset=utf-8
content-length: 12
etag: W/"c-Lve95gjOVATpfV8EL5X4nxwjKHE"
在响应 headers 中发送通知 HTTP/2
。