express
express copied to clipboard
Feature request: friendly exposure of req/res logic for HTTP/2 push promises
HTTP/2 has a great feature called push promises. It allows you to push files from server to client pro-actively because you know the client will need it. To do this responsibly, you can send along headers that can inform the client about the ETag for example, so the client can abort the file download if it's already in cache. This is all great, but very hard to deal with Express, because I would want to use all the configured Express logic in the push() API that (in my case) node-spdy expose.
In other words, I would love to combine res.sendFile() with res.push() (that node-spdy exposes). This is currently pretty much impossible as far as I can see. If Express could give more access to the internals of how a file would be delivered without actually delivering it (say, provide me with headers and a file stream I could pipe to res.push) that would be a major help. Alternative solutions of course welcome :)
Example spdy-server with Express:
const fs = require('fs');
const config = require('config');
const spdy = require('spdy');
const express = require('express');
const app = express();
const options = {
key: fs.readFileSync(config.get('server.ssl.key')),
cert: fs.readFileSync(config.get('server.ssl.cert'))
};
const httpServer = spdy.createServer(options, app);
httpServer.listen(8080);
// a route with a push promise stream:
app.get('/index.html', function (req, res) {
// index.html is guaranteed to need styles.css
// if *only* my code could look like this:
res.push('/styles.css').sendFile('./www/styles.css'));
res.sendFile('./www/index.html');
});
Unfortunately:
- Express' sendFile function really sends the file directly on the response stream. I wish I could send it on the push stream.
- node-spdy doesn't make this easier because of how its
pushmethod works (see push docs, cc @indutny).
The glue has to happen somewhere, and I'm not sure where or how. But without any changes to express and/or spdy, it seems impossible to do today.
i have https://github.com/jshttp/spdy-push, but it isn't updated for spdy@2, which supports http2. making it easy to push files is a matter of setting more headers. still wondering if it would be included in node, though...
node-spdy supports push() out of the box, and it seems easy enough to use. The problem is that express does a lot of logic for me to construct good responses to requests. I don't want to have to replicate though (nor have a different middleware solve it in its own way). I want to use express' internal logic, and simple API to deliver content through push. Ideally, doing a push is a one-liner, just like how most operations in Express are one-liners.
Is there a label or over-arching Issue mapped to http/2 support? I'd love to have it by default in Express.