Skip to content

Instantly share code, notes, and snippets.

@bellbind
Last active August 29, 2015 14:15
Show Gist options
  • Save bellbind/4aa0a39b16850e6f0d66 to your computer and use it in GitHub Desktop.
Save bellbind/4aa0a39b16850e6f0d66 to your computer and use it in GitHub Desktop.
[iojs][http2]HTTP/2 server with ECDSA cert
// $ npm install http2
// $ iojs server-with-push.js
// open "https://localhost:8080/" with FirefoxDeveloperEdition (37.0a2)
var fs = require("fs");
// openssl ecparam -genkey -name secp3841r1 -out ecdsa.key
// openssl req -new -key ecdsa.key -out ecdsa.req
// openssl req -x509 -key ecdsa.key -in ecdsa.req -out ecdsa.cer
var options = {
key: fs.readFileSync("./ecdsa.key"),
cert: fs.readFileSync("./ecdsa.cer"),
};
require("http2").createServer(options, function(req, res) {
console.log();
console.log(req.method + " " + req.url + " by " +
"HTTP Version:", req.httpVersion);
console.log(req.headers);
if (res.push) {
var pushing = res.push("/script.js");
sendScript(pushing);
console.log("Pushed /script.js");
res.push("/favicon.ico").end();
}
if (req.url === "/") {
sendHtml(res, req.httpVersion);
} else if (req.url === "/script.js") {
sendScript(res);
} else {
res.writeHead("404");
res.end();
}
}).listen(8080);
var sendHtml = function (res, version) {
res.writeHead("200", {
"content-type": "text/html; charset=UTF-8",
});
res.end("<body>HTTP/" + version +
": <script src='/script.js'></script></body>");
};
var sendScript = function (res) {
res.writeHead("200", {
"content-type": "text/javascript; charset=UTF-8",
});
res.end("document.write('Text From Script')");
};
// $ npm install http2
// $ iojs server.js
var fs = require("fs");
// openssl ecparam -genkey -name secp3841r1 -out ecdsa.key
// openssl req -new -key ecdsa.key -out ecdsa.req
// openssl req -x509 -key ecdsa.key -in ecdsa.req -out ecdsa.cer
var options = {
key: fs.readFileSync('./ecdsa.key'),
cert: fs.readFileSync('./ecdsa.cer'),
};
require('http2').createServer(options, function(request, response) {
response.end('Hello world!');
}).listen(8080);
// based on https://github.com/molnarg/node-http2 README snippet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment