[英]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: 我试图解决问题的事情:
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. 好的,经过彻底调查后,我在很大程度上找到了满意的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.