Description
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
push
method 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.