资讯专栏INFORMATION COLUMN

【搬运工】Say hello to HTTP/2 for Node.js Core

HelKyle / 1886人阅读

来源: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 :-)

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/84192.html

相关文章

  • [译]当 Node.js Core 遇到 HTTP/2

    摘要:几分钟前我打开了一个,它为提供了初始的实现。虽然还不堪用,但对来说是一个重要的里程碑。首先你需要跟着这个介绍来配置好的构建环境。我们付出的越多,就会变的越好。 原文:Say hello to HTTP/2 for Node.js Core 第一次尝试翻译文章,如果有翻译的不好或者有错误的地方还望大佬们指点一二,谢谢。 几分钟前我打开了一个 pull-request,它为 Nodejs ...

    Yuanf 评论0 收藏0
  • 微服务框架 Spark Framework

    摘要:我是广告本人的直播课程在月份就要开始了,希望小伙伴们支持一下,现在报名有优惠噢 源码:http://git.oschina.net/sancha... Spark Framework beetl fastjson 结合 项目结构如下 showImg(https://segmentfault.com/img/bVP12A?w=315&h=512); pom.xml如下: 4...

    fasss 评论0 收藏0
  • Node.js原生开发入门完全教程

    摘要:原生开发入门完全教程微信公众号开发企业级产品全栈开发速成周末班首期班号正式开班,欢迎抢座一关于本篇文章参考了入门并从零到壹操作了一遍,感谢原作者,同时也强烈推荐大家移步到原文给予原文作者一个赞赏支持。 Node.js原生开发入门完全教程 (Node+Vue+React+微信公众号开发)企业级产品全栈开发速成周末班首期班(10.28号正式开班,欢迎抢座) 一、关于 本篇文章参考了Node...

    alphahans 评论0 收藏0
  • 初识 CoffeeScript

    摘要:而造成一些莫名其妙的错误。写一个文件打印出编译命令会在同级目录下生成一个同名的文件。将包裹在了一个匿名函数当中,并用调用,这样使得代码隔离,不会和外部混淆。其中的表示的就是为了方便使用,可以使用双冒号来替代。 很早就知道这CoffeeScript一门语言,但是一直没有机会系统的学习下,那天趁在公司没有什么要紧的项目做,就根据CoffeeScript首页的例子学了一下。 引用Coffe...

    骞讳护 评论0 收藏0
  • node函数使用

    摘要:在中,一个函数可以作为另一个函数的参数。我们可以先定义一个函数,然后传递,也可以在传递参数的地方直接定义函数。中函数的使用与类似。以上代码中,我们把函数作为函数的第一个变量进行了传递。 在JavaScript中,一个函数可以作为另一个函数的参数。我们可以先定义一个函数,然后传递,也可以在传递参数的地方直接定义函数。 Node.js中函数的使用与Javascript类似。 funct...

    番茄西红柿 评论0 收藏0

发表评论

0条评论

HelKyle

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<