2016-02-17 15 views
15

NodeJS 4.x veya 5.x HTTP/2 protokolünü doğal olarak destekliyor mu? Http2 paketinin olduğunu biliyorum ama bu harici bir şey.NodeJS yerel http2 desteği

Düğümün çekirdeğine http2 desteğini birleştirmek için bazı planlar var mı?

cevap

7

--expose-http2 bayrak deney HTTP2 destek sağlar: Burada

çekirdek NodeJS HTTP/2 destek ekleme hakkında tartışma. Bu bayrak, 5 Ağustos 2017'den beri ( pull request) gece yapımında (Node v8.4.0) kullanılabilir.

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 bayrağı da Düğüm v8.5.0 yana eklenebilir.

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

client.mjs

import http2 from 'http2'; 

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

test etmek için NVS (düğüm şekli Değiştirici) gece oluşturur kullanın.

nvs add nightly 
nvs use nightly 
+0

SSL bağlantısı yapabilmek için kendinden imzalı sertifikalar için 'process.env.NODE_TLS_REJECT_UNAUTHORIZED =" 0 "; –

+0

Ayrıca, Node.js belgelerinde güncel istemci tarafı örneğine bakın: https://nodejs.org/api/http2.html#http2_client_side_example. –

1

Düğüm 8.4.0 Deneysel Http2 API vardır. Docs here nodejs http2

+0

nodejs'deki http2 desteği artık deneysel olmayacak mı? – funkenstrahlen

2

Kodunuzu çalıştırırken v8.8.1 düğümünden --expose-http2 bayrağına ihtiyacınız yoktur.

HTTP/2 ile çalışmaya başlamanın en basit yolu, Node.js'nin ortaya koyduğu uyumluluk API'sini kullanmaktır.

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'u kullanma hakkında daha fazla yazı yazdım.

+0

Ayrıca, Node.js belgelerinde güncel sunucu tarafı örneğine bakın: https://nodejs.org/api/http2.html#http2_server_side_example. –