I am learning how to build an http2 server with NodeJS 10 LTS official documentation. 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.
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:
- As required in the official documentation I have created private and public certificate (.pem).
- I have included the public certificate inside postman software. So now the only error I receive is the one mentioned above (Unknown ALPN protocol).
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.