使用 node new http2 服务器时,尝试从浏览器调用它时遇到此错误:ERR_INVALID_HTTP_RESPONSE。

编码:

const http2 = require('http2');

// Create a plain-text HTTP/2 server
const server = http2.createServer();

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

server.listen(80);

最佳答案

事实证明,chrome 不允许您访问不安全的 http2 服务器,我不得不将代码更改为:

const server = http2.createSecureServer({
  key,
  cert
});

然后它起作用了。

关于node.js - nodejs http2 上的 ERR_INVALID_HTTP_RESPONSE,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45551939/

10-11 07:30