I'm (re-)learning node.js and was was wondering if I should use the http2 module for new stuff - just like you should use ES6+ for new stuff - or is it for special cases and you should normally stick to the http & https modules?I'm not that familiar with HTTP/2 besides that it has lower overhead because it uses a binary format instead of plaintext and you need TLS, because no browser has supports without it.My problem is, that I can't find that many newbie friendly information on the web for it, is there any good resource?e.g. I can't even make the Hello World example from the http2-docs fail-safe: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('
Hello World
'); }); server.listen(8443); Which works fine, with curl:➜ curl -k https://localhost:8443/
Hello World
% until I fetch only the headers:➜ curl -k -I https://localhost:8443/ curl: (56) OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 104 node.js throws:events.js:170 throw er; // Unhandled 'error' event ^ Error [ERR_STREAM_WRITE_AFTER_END]: write after end at writeAfterEnd (_stream_writable.js:248:14) at ServerHttp2Stream.Writable.write (_stream_writable.js:296:5) at ServerHttp2Stream.Writable.end (_stream_writable.js:584:10) // ... Which is probably, because curl closes the stream immediately after receiving the header?If I remove the body, it works. stream.respond({ 'content-type': 'text/html', ':status': 200 }); stream.end(); // no '
Hello World
' Obviously - in the vast majority of cases - I want to send a body.
Submitted June 03, 2019 at 01:56AM by 0xnoob
No comments:
Post a Comment