Skip to content

Instantly share code, notes, and snippets.

@minrwhite
Created March 21, 2016 11:13
Show Gist options
  • Save minrwhite/f667d8db5a1ad837eabe to your computer and use it in GitHub Desktop.
Save minrwhite/f667d8db5a1ad837eabe to your computer and use it in GitHub Desktop.
Node file for testing http2 cross-origin push
var http2 = require('http2'),
fs = require('fs'),
url = require('url'),
options = {
key: fs.readFileSync('./ssl/server.key'),
cert: fs.readFileSync('./ssl/server.cert')
};
http2.createServer(options, function(request, response) {
if (url.parse(request.url).pathname === '/mystyles.css') {
response.end('/* example */ h1 {color: #802828;}');
return;
}
var cssResponse = response.push({':authority': 'test:8080', 'method': 'get', 'path': '/mystyles.css'});
response.end('<!DOCTYPE html>' +
'<html>' +
'<head>' +
'<meta charset="utf-8">' +
'<link rel="stylesheet" href="https://test:8080/mystyles.css"/>' +
'<script type="application/javascript">(function(){' +
' window.onload = function() {' +
' [].forEach.call(document.getElementsByTagName("link"), function(elem){elem.setAttribute("rel", "stylesheet")})' +
' }' +
'})()</script>' +
'</head>' +
'<body>' +
'<h1>This appears red if the css file is fetched from the server, or green if it is pushed</h1>' +
'</body>' +
'</html>');
cssResponse.end('/* example */ h1 {color: #288028;}');
}).listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment