molnarg / node-http2

An HTTP/2 client and server implementation for node.js
MIT License
1.79k stars 187 forks source link

Express HTTP2 #220

Open perryjsteward opened 7 years ago

perryjsteward commented 7 years ago

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

ghost commented 7 years ago

Same error here. (node@v6.7.0, express@5.0.0-alpha.2, http2@3.3.6)

waterfoul commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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

waterfoul commented 7 years ago

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

Jashepp commented 7 years ago

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 :)

ghost commented 7 years ago

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