node-http2 icon indicating copy to clipboard operation
node-http2 copied to clipboard

Express HTTP2

Open perryjsteward opened this issue 9 years ago • 7 comments

Hi,

sorry for opening another issue on this but the past one is locked and hasnt been updated for a year. was there ever a resolution found for Express and HTTP2 module?

I'm still getting the error mentioned in previous comments.

image

perryjsteward avatar Sep 29 '16 12:09 perryjsteward

Same error here. ([email protected], [email protected], [email protected])

ghost avatar Sep 29 '16 21:09 ghost

I just figured out what's happening (Digging through the code to find this took 2-3 hrs). The fault is actually express'. If you look at express/lib/middleware/init.js on lines 28 and 29 they are replacing the prototypes of the request and response. I don't know why but that is causing the http2 prototypes to get destroyed which is causing that error. If you fix the request's prototype you actually see similar issues with the response object.

waterfoul avatar Oct 20 '16 07:10 waterfoul

More Info. That behavior has been there since express 3.0.0... Still digging into why Update: I can't seem to figure out why they did that but express doesn't work without it......

waterfoul avatar Oct 20 '16 07:10 waterfoul

It appears that there has been some discussion going on in the express side https://github.com/expressjs/express/issues/2761

waterfoul avatar Oct 20 '16 07:10 waterfoul

adding

express.request.__proto__ = http2.IncomingMessage.prototype;
express.response.__proto__ = http2.ServerResponse.prototype;

per one of the express post suggestions seems to work around the issue for now

waterfoul avatar Oct 20 '16 07:10 waterfoul

As the above code does work nicely, it does stop HTTP/1.x requests from working if the http server is used with express too (since the prototype is now changed to http2's). I have made a npm module which creates new instances of express's request and response objects, and does the above prototype change only for http2 requests (via a middleware). https://www.npmjs.com/package/express-http2-workaround More explanation of how it works is in the package's readme. It works with the latest http2 and express packages. I hope this helps :)

Jashepp avatar Nov 13 '16 05:11 Jashepp

I will try @waterfoul and @Unchosen solutions when I have some time this week. Thank you guys

ghost avatar Dec 14 '16 21:12 ghost