简体   繁体   English

简单的http2 NodeJS服务器来自官方文档和邮递员无法正常工作

[英]Simple http2 NodeJS Server from official docs & postman not working

I am learning how to build an http2 server with NodeJS 10 LTS official documentation . 我正在学习如何使用NodeJS 10 LTS 官方文档构建一个http2服务器。 I copy pasted the server side code into server.js and run node on it, but when I try to connect with postman (REST testing tool) I receive an error. 我将服务器端代码粘贴到server.js并在其上运行节点,但是当我尝试连接postman(REST测试工具)时,我收到一个错误。

const http2 = require('http2');
const fs = require('fs');

const server = http2.createSecureServer({
  key: fs.readFileSync('localhost-privkey.pem'),
  cert: fs.readFileSync('localhost-cert.pem')
});
server.on('error', (err) => console.error(err));

server.on('stream', (stream, headers) => {
  // stream is a Duplex
  stream.respond({
    'content-type': 'text/html',
    ':status': 200
  });
  stream.end('<h1>Hello World</h1>');
});

server.listen(8443);

The error I receive from postman is as follows: 我从邮递员收到的错误如下:

Unknown ALPN Protocol, expected `h2` to be available.
If this is a HTTP request: The server was not configured with the `allowHTTP1` option or a listener for the `unknownProtocol` event.

Things I have tried to solve the problem: 我试图解决问题的事情:

  1. As required in the official documentation I have created private and public certificate (.pem). 根据官方文档的要求,我创建了私有和公共证书(.pem)。
  2. I have included the public certificate inside postman software. 我在邮递员软件中加入了公共证书。 So now the only error I receive is the one mentioned above (Unknown ALPN protocol). 所以现在我收到的唯一错误是上面提到的错误(未知的ALPN协议)。

What else is needed to make the example in the official docs work? 还有什么需要使官方文档中的示例工作? I could not find online resources for that, and all the previous questions on stackoverflow relates to old versions of NodeJS when http2 was not still native. 我无法找到相关的在线资源,并且当http2不是原生的时,stackoverflow上的所有先前问题都与旧版本的NodeJS有关。

Try to add allowHTTP1: true in Server options at it says server not configure with the allowHTTP1 尝试在服务器选项中添加allowHTTP1:true,表示服务器未配置allowHTTP1

const server = http2.createSecureServer({
  key: fs.readFileSync('localhost-privkey.pem'),
  cert: fs.readFileSync('localhost-cert.pem')
});

To

const server = http2.createSecureServer({
  key: fs.readFileSync('localhost-privkey.pem'),
  cert: fs.readFileSync('localhost-cert.pem'),
  allowHTTP1: true
});

From github http2 documentation : 来自github http2文档:

allowHTTP1 {boolean} Incoming client connections that do not support HTTP/2 will be downgraded to HTTP/1.x when set to true. allowHTTP1 {boolean}当设置为true时,不支持HTTP / 2的传入客户端连接将降级为HTTP / 1.x. See the 'unknownProtocol' event. 请参阅'unknownProtocol'事件。 See ALPN negotiation. 请参阅ALPN协商。 Default: false. 默认值:false。

Here i found better answer already on Stackoverflow Configure HTTP2 NodeJS Server 在这里,我在Stackoverflow 配置HTTP2 NodeJS服务器上找到了更好的答案

Ok, after thorough investigation I found a satisfactory answer to a great extent. 好的,经过彻底调查后,我在很大程度上找到了满意的答案。

  1. The https request should be https://localhost:8443/stream to get a response from the server with Hello World. https请求应为https:// localhost:8443 / stream ,以便从具有Hello World的服务器获取响应。 Without the stream path there is no response. 没有流路径就没有响应。
  2. postman gives 403 response after installing the public certificate, but insomnia doesn't give any response. 邮递员在安装公共证书后给出403回复,但失眠不给予任何回应。
  3. Using google chrome developer tools in the Network tab I can finally get response 200 OK from the server as shown below. 使用网络选项卡中的谷歌浏览器开发人员工具,我终于可以从服务器获得200 OK,如下所示。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM