簡體   English   中英

NodeJS本機http2支持

[英]NodeJS native http2 support

NodeJS 4.x或5.x本機是否支持HTTP / 2協議? 我知道有http2包,但它是一個外部的東西。

是否有計划將http2支持合並到Node的核心?

--expose-http2 flag支持實驗性--expose-http2 自2017年8月5日起,此標志可用於夜間構建(Node v8.4.0)( 拉取請求 )。

node --expose-http2 client.js

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();

也可以從Node v8.5.0開始添加--experimental-modules標志。

node --expose-http2 --experimental-modules client.mjs

client.mjs

import http2 from 'http2';

const client = http2.connect('https://stackoverflow.com');

我使用NVS(節點版本切換器)來測試夜間構建。

nvs add nightly
nvs use nightly

還沒有。

以下是關於向核心NodeJS添加HTTP / 2支持的討論: https//github.com/nodejs/NG/issues/8

從節點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);

我已經寫了更多關於使用本機HTTP / 2 Node.js公開來創建服務器的信息

Node 8.4.0有一個實驗性的Http2 API。 這里的文件是nodejs http2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM