來源:Say hello to HTTP/2 for Node.js Core – Node.js Collection – Medium
A few minutes ago I opened the initial pull-request that would provide an implementation of HTTP/2 for Node.js core. While it’s far from being production ready, this marks a key milestone.
Because this is just a pull-request, it’s possible to play around with, but there are a few additional steps.
First, you’ll need to make sure you’re set up for building Node.js locally by following the instructions here: https://github.com/nodejs/nod...
After that, check out the working branch:
$ git clone https://github.com/jasnell/node $ git checkout initial-pr
Then build...
$ ./configure $ make -j8
Warning, building Node.js from scratch takes quite a bit of time. So go grab a quick snack while things get going.
Once complete, you can create a functioning HTTP/2 server in a few lines of code:
const http2 = require("http2"); const server = http2.createServer(); server.on("stream", (stream, requestHeaders) => { stream.respond({ ":status": 200, "content-type": "text/plain" }); stream.write("hello "); stream.end("world"); }); server.listen(8000);
Because the HTTP/2 support is still experimental, in order to run this server, the Node.js instance must be started using the --expose-http2 command line argument:
$ node --expose-http2 h2server.js
Note that the server above uses plain-text TCP connection so the server will not be accessible from Web Browsers, which require using TLS. We can, however, create a simple HTTP/2 client:
const http2 = require("http2"); const client = http2.connect("http://localhost:8000"); const req = client.request({ ":method": "GET", ":path": "/" }); req.on("response", (responseHeaders) => { // do something with the headers }); req.on("data", (chunk) => { // do something with the data }); req.on("end", () => client.destroy());
Setting up a TLS-enabled HTTP/2 server requires just a few more additional steps:
const http2 = require("http2"); const options = { key: getKeySomehow(), cert: getCertSomehow() }; const server = http2.createSecureServer(options); server.on("stream", (stream, requestHeaders) => { stream.respond(); stream.end("secured hello world!"); }); server.listen(43);
Refer to the Node.js tls.createServer() documentation for more information regarding the required key and cert configuration options.
While there are many details that still need to worked through, and likely many issues that need to be fixed… this initial implementation provides enough functionality to get started, including:
Push Stream Support
respondWithFile() and respondWithFD() APIs that allow extremely efficient sending of raw file data that bypasses the Streams API.
TLS and Plain-text connections
Full support for stream multiplexing
HTTP/2 Prioritization and Flow Control
Support for HTTP/2 trailers
HPACK header compression support
A compatibility API layer that operates as close as possible to the existing HTTP/1 API
Development will be ongoing, as will security hardening, performance optimization, and API refinement. The more input we get on this, the better it will become.
Happy Multiplexing to All :-)
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/84192.html
摘要:幾分鐘前我打開了一個,它為提供了初始的實現。雖然還不堪用,但對來說是一個重要的里程碑。首先你需要跟著這個介紹來配置好的構建環境。我們付出的越多,就會變的越好。 原文:Say hello to HTTP/2 for Node.js Core 第一次嘗試翻譯文章,如果有翻譯的不好或者有錯誤的地方還望大佬們指點一二,謝謝。 幾分鐘前我打開了一個 pull-request,它為 Nodejs ...
摘要:我是廣告本人的直播課程在月份就要開始了,希望小伙伴們支持一下,現在報名有優惠噢 源碼:http://git.oschina.net/sancha... Spark Framework beetl fastjson 結合 項目結構如下 showImg(https://segmentfault.com/img/bVP12A?w=315&h=512); pom.xml如下: 4...
摘要:原生開發入門完全教程微信公眾號開發企業級產品全棧開發速成周末班首期班號正式開班,歡迎搶座一關于本篇文章參考了入門并從零到壹操作了一遍,感謝原作者,同時也強烈推薦大家移步到原文給予原文作者一個贊賞支持。 Node.js原生開發入門完全教程 (Node+Vue+React+微信公眾號開發)企業級產品全棧開發速成周末班首期班(10.28號正式開班,歡迎搶座) 一、關于 本篇文章參考了Node...
摘要:而造成一些莫名其妙的錯誤。寫一個文件打印出編譯命令會在同級目錄下生成一個同名的文件。將包裹在了一個匿名函數當中,并用調用,這樣使得代碼隔離,不會和外部混淆。其中的表示的就是為了方便使用,可以使用雙冒號來替代。 很早就知道這CoffeeScript一門語言,但是一直沒有機會系統的學習下,那天趁在公司沒有什么要緊的項目做,就根據CoffeeScript首頁的例子學了一下。 引用Coffe...
閱讀 1370·2021-11-22 09:34
閱讀 2581·2021-11-12 10:36
閱讀 1111·2021-11-11 16:55
閱讀 2324·2020-06-22 14:43
閱讀 1457·2019-08-30 15:55
閱讀 1975·2019-08-30 15:53
閱讀 1764·2019-08-30 10:50
閱讀 1217·2019-08-29 12:15