Node.js NodeJS本机http2支持
NodeJS 4.x或5.x是否本机支持HTTP/2协议?我知道有http2软件包,但它是一个外部的东西 是否有将http2支持合并到节点核心的计划?没有,还没有 下面是关于向核心节点添加HTTP/2支持的讨论:Node.js NodeJS本机http2支持,node.js,http2,Node.js,Http2,NodeJS 4.x或5.x是否本机支持HTTP/2协议?我知道有http2软件包,但它是一个外部的东西 是否有将http2支持合并到节点核心的计划?没有,还没有 下面是关于向核心节点添加HTTP/2支持的讨论:--expose-http2标志启用了实验性的http2支持。自2017年8月5日起,该标志可用于夜间构建(节点v8.4.0)() client.js const http2 = require('http2'); const client = http2.connect('https:
--expose-http2
标志启用了实验性的http2支持。自2017年8月5日起,该标志可用于夜间构建(节点v8.4.0)()
client.js
const http2 = require('http2');
const client = http2.connect('https://stackoverflow.com');
const req = client.request();
req.setEncoding('utf8');
req.on('response', (headers, flags) => {
console.log(headers);
});
let data = '';
req.on('data', (d) => data += d);
req.on('end', () => client.destroy());
req.end();
——从节点v8.5.0开始,还可以添加实验模块
标志
node --expose-http2 --experimental-modules client.mjs
客户.mjs
import http2 from 'http2';
const client = http2.connect('https://stackoverflow.com');
我使用NVS(节点版本切换器)来测试夜间构建
nvs add nightly
nvs use nightly
Node 8.4.0有一个实验性的Http2 API。这里的文档来自节点v8.8.1,运行代码时不需要
--expose-http2
标志
开始使用HTTP/2的最简单方法是使用Node.js公开的兼容API
const http2 = require('http2');
const fs = require('fs');
const options = {
key: fs.readFileSync('./selfsigned.key'),
cert: fs.readFileSync('./selfsigned.crt'),
allowHTTP1: true
}
const server = http2.createSecureServer(options, (req, res) => {
res.setHeader('Content-Type', 'text/html');
res.end('ok');
});
server.listen(443);
我已经写了更多关于使用的文章 不要忘记添加
process.env.NODE\u TLS\u REJECT\u UNAUTHORIZED=“0”
,使自签名证书能够进行SSL连接。另请参阅Node.js文档中的最新客户端示例:。另请参阅Node.js文档中的最新服务器端示例:。您知道nodejs中的http2支持何时不再是实验性的吗?
const http2 = require('http2');
const fs = require('fs');
const options = {
key: fs.readFileSync('./selfsigned.key'),
cert: fs.readFileSync('./selfsigned.crt'),
allowHTTP1: true
}
const server = http2.createSecureServer(options, (req, res) => {
res.setHeader('Content-Type', 'text/html');
res.end('ok');
});
server.listen(443);