Node: Using express with the new http2 module

Created on 7 Aug 2017  Â·  16Comments  Â·  Source: nodejs/node

  • Version: v9.0.0-pre
  • Platform: MacOS
  • Subsystem: http2

Testing node's new http2 native module I couldn't get express to serve requests over http2,
Using node master build with --expose-http2 (v9.0.0-pre macOS) flag:

const express = require('express');

const app = express();

app.get('/express', (req, res) => {
  res.send('Hello from express');
});

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

When requesting /express the server crashed with the following error:

(node:80731) ExperimentalWarning: The http2 module is an experimental API.
_http_incoming.js:104
  if (this.socket.readable)
                 ^

TypeError: Cannot read property 'readable' of undefined
    at IncomingMessage._read (_http_incoming.js:104:18)
    at IncomingMessage.Readable.read (_stream_readable.js:431:10)
    at IncomingMessage.read (_http_incoming.js:96:15)
    at resume_ (_stream_readable.js:811:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

Opened an issue for express as well: https://github.com/expressjs/express/issues/3388
@benjamingr

http2

All 16 comments

Ping @jasnell - this is a first time contributor trying http2.

Playing with it a little more - I got it to work, but I had to expose Http2ServerRequest/Response, I think it might be a good idea to expose it in general in the new http2 module.

There is a matching PR to express, that I am waiting for go / no go (still
needs refactoring etc.) but with it - http2 works including push.

On Thu, Aug 31, 2017 at 5:57 AM, MongoExpUser notifications@github.com
wrote:

So, what is the solution to this problem as of today?

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/nodejs/node/issues/14672#issuecomment-326175455, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AOq55S76pwgWv4VtE_rB5m5TjXquGeFYks5sdiEugaJpZM4OvuHm
.

--
Pini Houri
Web Developer
Mobile: +972-525402819
Email: pini.[email protected]

[image: cid:[email protected]]
http://www.dynamicyield.com/?utm_source=signature&utm_medium=email&utm_campaign=logo

@PiniH any ETA on when we can expect Express to work with http2?

Wrote to Douglass on the corresponding express PR will update once I get a word from him.

On Thu, Aug 31, 2017 at 10:30 AM +0300, "Benjamin Gruenbaum" notifications@github.com wrote:

@PiniH any ETA on when we can expect Express to work with Http2?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

@dougwilson

(unrelated: I have removed excess spacing created by replying via email without altering the content in PiniH's post)

@PiniH,
Thanks for the updates.

Has the change to expose the prototype been released yet?

Not yet. It has landed tho so it should be soon

Nice :) also did anything ever happen with the two items at https://github.com/nodejs/node/pull/14690#issuecomment-321756459 ?

I'll look at the statusMessage setter. The path one is likely better for express to handle but if you have an idea on how we can handle it on our side then ++

Thanks, James :) I believe @PiniH is going to look into how / if Express.js can handle the req.path on it's side, so it may be possible.

Awesome. I'm excited. Let me know if there's anything else needed for this.

Any chance of getting an example of how you got it working?

I tired...

const {
           Http2ServerRequest,
           Http2ServerResponse,
} = http2;
this.express.use((req, res, next)=>{
            Object.setPrototypeOf(req, Http2ServerRequest.prototype);
            Object.setPrototypeOf(res, Http2ServerResponse.prototype);
            next();
});
this.server = http2.createSecureServer({
            key: fs.readFileSync(process.env.KEY_FILE),
            cert: fs.readFileSync(process.env.CERT_FILE),
            allowHTTP1: true
}, this.express);

Playing with it a little more - I got it to work, but I had to expose Http2ServerRequest/Response, I think it might be a good idea to expose it in general in the new http2 module.

@PiniH (or anyone else), can you please explain your solution? I'm getting the same error now and I don't see how you were able to get it working.

  • Node 10.16
  • Express ~4.16.1.

My simple server:

// index.js

const port = 3000
const express = require('express')
const http2 = require('http2')

const app = express();

app.get('*', (req, res) => {
  res.status(200).json({message: 'ok'})
})

const server = http2.createServer(app)
server.listen(port)
server.on('listening', () => console.log(`Listening on port: ${port}.`))

I'm using http2.createServer instead of http2.createSecureServer because I'm using a reverse proxy and the TLS encryption happens there, so it must not be encrypted here.

Here's what I get when I run the server locally and hit it with cURL:

$ curl --http2-prior-knowledge http://localhost:3000
curl: (56) Recv failure: Connection reset by peer

and on the server I see the error reported at the top of this issue:

_http_incoming.js:95
  if (this.socket.readable)
                  ^

TypeError: Cannot read property 'readable' of undefined
    at IncomingMessage._read (_http_incoming.js:95:19)
    at IncomingMessage.Readable.read (_stream_readable.js:453:10)
    at resume_ (_stream_readable.js:929:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)

I get the same error when I deploy it. It successfully navigates the TLS handshake on the reverse proxy, then it hits my server and crashes with the same error.

If anyone can help me get unstuck here I would be very grateful. Thanks for all you do.

Whats up with this, pushed to express then whats the corresponding issue there ?
Its still a issue.

/T

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mcollina picture mcollina  Â·  3Comments

vsemozhetbyt picture vsemozhetbyt  Â·  3Comments

willnwhite picture willnwhite  Â·  3Comments

danialkhansari picture danialkhansari  Â·  3Comments

Icemic picture Icemic  Â·  3Comments