Skip to content

Instantly share code, notes, and snippets.

@deanhume
Last active October 24, 2016 12:48
Show Gist options
  • Save deanhume/0e9befe56a41b43be9859fa8bd92de19 to your computer and use it in GitHub Desktop.
Save deanhume/0e9befe56a41b43be9859fa8bd92de19 to your computer and use it in GitHub Desktop.
HTTP/2 Server Push
const spdy = require('spdy');
const express = require('express');
const fs = require('mz/fs');
const app = express();
app.use(express.static('public'));
app.get('/home', (req, res) => {
Promise.all([
fs.readFile('home.html'),
fs.readFile('public/js/squareRoot.js'),
fs.readFile('public/js/randomNumber.js'),
]).then(files => {
// Does the browser support push?
if (res.push){
// The JS file
var squareRootStream = res.push('/js/squareRoot.js', {
req: {'accept': '**/*'},
res: {'content-type': 'application/javascript'}
});
squareRootStream.on('error', err => {
console.log(err);
});
squareRootStream.end(files[1]);
// The Image
var randomNumberStream = res.push('/js/randomNumber.js', {
req: {'accept': '**/*'},
res: {'content-type': 'application/javascript'}
});
randomNumberStream.on('error', err => {
console.log(err);
});
randomNumberStream.end(files[2]);
}
res.writeHead(200);
res.end(files[0]);
}).catch(error => res.status(500).send(error.toString()));
});
spdy.createServer({
key: fs.readFileSync('./server.key'),
cert: fs.readFileSync('./server.crt')
}, app)
.listen(8000, (err) => {
if (err) {
throw new Error(err);
}
console.log('Listening on port: 8000.');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment