2016-02-17 40 views
15

NodeJS 4.xまたは5.xはネイティブにHTTP/2プロトコルをサポートしていますか?私はhttp2パッケージがあることを知っていますが、それは外的なものです。NodeJSネイティブhttp2サポート

http2サポートをノードのコアにマージする計画はありますか?

答えて

7

--expose-http2フラグは、実験HTTP2サポートを可能にします。このフラグは、夜間のビルド(ノードv8.4.0)で、Aug 5、2017(pull request)以降に使用できます。

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

--experimental-modulesフラグは、ノードのV8.5.0以降に追加することができます。私はテストのためにNVS(ノードバージョンスイッチャー)を使用

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

client.mjs

import http2 from 'http2'; 

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

はナイトリービルド。

nvs add nightly 
nvs use nightly 
+0

です。自己署名証明書がSSL接続できるようにするには、 'process.env.NODE_TLS_REJECT_UNAUTHORIZED =" 0 ";'を追加することを忘れないでください。 –

+0

Node.jsのドキュメント(https://nodejs.org/api/http2.html#http2_client_side_example)の最新のクライアント側の例も参照してください。 –

8

いいえ、まだありません。ここ

コアNodeJSにHTTP/2サポートを追加することについての議論である。https://github.com/nodejs/NG/issues/8

+0

リポジトリはhttps://github.com/nodejs/http2 – pungggi

1

ノード8.4.0には実験的なHttp2 APIがあります。ここにあるドキュメントnodejs http2

+0

nodejでのhttp2サポートがもう実験的ではないことを知っていますか? – funkenstrahlen

2

ノード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); 

私はnative HTTP/2 Node.js exposes to create a server hereの使用についてさらに詳しく書いています。

+0

Node.jsのドキュメント(https://nodejs.org/api/http2.html#http2_server_side_example)の最新のサーバー側の例も参照してください。 –

関連する問題