Node.js 每年进行两次大的发布,2017年10月,Node.js发布了 9.0 版本,与此同时, Node.js 8.9.0 成为了最新的 LTS 版本。这意味着对 8.9.0 的支持将会维持到2019年底,此后的一个LTS版本将会是 Node.js 10
查看 Node.js 发布历史
Node.js 9 有什么新特性?
http/2
Node 8.4 首次支持了http/2
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);
但由于仍处于实验性阶段,运行时需要加上—expose-http2
参数
$ node --expose-http2 h2server.js
而在node 9中去除了这一参数,可以直接使用
$ node h2server.js
当然由于现在主流浏览器只有在HTTPS时才启用HTTP2,我们要启动一个https的服务器,可以参考如何在本地搭建https服务器
其他关于http/2的变化还包括:
- 新增对 ALTSVC(HTTP Alternative Services) 的支持
- 新增
maxSessionMemory
,限制单个http2线程允许使用的内存上限,一旦超过这个值,http2请求将被拒绝 - 收集并报告有关
Http2Session
和Http2Stream
实例的基本计时信息。 - 改进了
Http2Stream
和Http2Session
的关闭方式,Http2Stream.prototype.rstStream()
方法被移动至Http2Stream.prototype.close()
中
- 在
Http2Session
上引入了新的属性来确定会话是否安全