如何在Node.Js中启用HTTP/2.0?


本文摘自PHP中文网,作者不言,侵删。

node-http2是一个节点模块,为nodejs应用程序提供HTTP/2协议的客户端和服务器实现。此节点API与扩展支持HTTP/2的节点HTTPS模块非常相似。

安装Node.Js

如果已经在系统上安装了node.js,则可以跳过此步骤。如果系统上没有node.js,请使用以下命令安装。

1

2

3

4

$ sudo apt-get install python-software-properties python g++ make

$ curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -

$ sudo apt-get update

$ sudo apt-get install nodejs

当然也可以通过NPM升级node.js。

安装Node-HTTP2模块

node-http2模块在默认的npm库下可用。因此,只需执行以下命令即可为应用程序安装它。

1

$ npm install http2

创建node服务器

让我们创建一个支持HTTP/2的示例节点服务器。首先创建自定义的SSL证书或从授权的SSL提供程序获取有效的SSL。

1

$ openssl req -x509 -nodes -newkey rsa:2048 -keyout example.com.key -out example.com.crt

现在创建包含以下内容的http2-server.js文件。

1

2

3

4

5

6

7

8

9

10

var fs = require('fs');

var options = {

  key: fs.readFileSync('./example.com.key'),

  cert: fs.readFileSync('./example.com.crt')

};

  

require('http2').createServer(options, function(request, response) {

  response.end('Welcome HTTP/2.0');

  console.log("Server listening on: http://localhost:8000");

}).listen(8000);

启动node服务器

让我们使用以下命令启动node.js服务器。它将在系统的端口8000上启动Web服务器。

1

$ node http2-server.js

并在端口8000上访问localhost。

本篇文章到这里就已经全部结束了,更多其他精彩内容可以关注PHP中文网的node.js教程视频栏目!

以上就是如何在Node.Js中启用HTTP/2.0?的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

node.js是什么?优势是什么?

node.js 安装配置

了解一下node.js casbin

nodejs模块的简单理解(附示例)

node.js爬取豆瓣数据实例

避免node.js模块日志污染程序日志的方法介绍

了解node.js中的process对象

node.js如何使用文件系统模块?常用fs模块方法介绍

深入了解node.js中的koa框架

两种升级nodejs的方法介绍

更多相关阅读请进入《node.js》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...