Skip to content

Instantly share code, notes, and snippets.

@devinivy
Created November 22, 2016 20:59
Show Gist options
  • Save devinivy/0e1ee255662fc1e37470b57cdf85050f to your computer and use it in GitHub Desktop.
Save devinivy/0e1ee255662fc1e37470b57cdf85050f to your computer and use it in GitHub Desktop.
Underdog with vision
'use strict';
const Hapi = require('hapi');
const Underdog = require('underdog');
const Vision = require('vision');
const Handlebars = require('handlebars');
const Spdy = require('spdy');
const Http2 = require('http2');
const agent = new Http2.Agent({ rejectUnauthorized: false });
const creds = {
key: '-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEA0UqyXDCqWDKpoNQQK/fdr0OkG4gW6DUafxdufH9GmkX/zoKz\ng/SFLrPipzSGINKWtyMvo7mPjXqqVgE10LDI3VFV8IR6fnART+AF8CW5HMBPGt/s\nfQW4W4puvBHkBxWSW1EvbecgNEIS9hTGvHXkFzm4xJ2e9DHp2xoVAjREC73B7JbF\nhc5ZGGchKw+CFmAiNysU0DmBgQcac0eg2pWoT+YGmTeQj6sRXO67n2xy/hA1DuN6\nA4WBK3wM3O4BnTG0dNbWUEbe7yAbV5gEyq57GhJIeYxRvveVDaX90LoAqM4cUH06\n6rciON0UbDHV2LP/JaH5jzBjUyCnKLLo5snlbwIDAQABAoIBAQDJm7YC3pJJUcxb\nc8x8PlHbUkJUjxzZ5MW4Zb71yLkfRYzsxrTcyQA+g+QzA4KtPY8XrZpnkgm51M8e\n+B16AcIMiBxMC6HgCF503i16LyyJiKrrDYfGy2rTK6AOJQHO3TXWJ3eT3BAGpxuS\n12K2Cq6EvQLCy79iJm7Ks+5G6EggMZPfCVdEhffRm2Epl4T7LpIAqWiUDcDfS05n\nNNfAGxxvALPn+D+kzcSF6hpmCVrFVTf9ouhvnr+0DpIIVPwSK/REAF3Ux5SQvFuL\njPmh3bGwfRtcC5d21QNrHdoBVSN2UBLmbHUpBUcOBI8FyivAWJhRfKnhTvXMFG8L\nwaXB51IZAoGBAP/E3uz6zCyN7l2j09wmbyNOi1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dHkgTHRkMB4XDTE0MDEyNTIxMjIxOFoXDTE1MDEyNTIxMjIxOFowRTELMAkG\nA1UEBhMCVVMxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0\nIFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\nANFKslwwqlgyqaDUECv33a9DpBuIFug1Gn8Xbnx/RppF/86Cs4P0hS6z4qc0hiDS\nlrcjL6O5j416qlYBNdCwyN1RVfCEen5wEU/gBfAluRzATxrf7H0FuFuKbrwR5AcV\nkltRL23nIDRCEvYUxrx15Bc5uMSdnvQx6dsaFQI0RAu9weyWxYXOWRhnISsPghZg\nIjcrFNA5gYEHGnNHoNqVqE/mBpk3kI+rEVzuu59scv4QNQ7jegOFgSt8DNzuAZ0x\ntHTW1lBG3u8gG1eYBMquexoSSHmMUb73lQ2l/dC6AKjOHFB9Ouq3IjjdFGwx1diz\n/yWh+Y8wY1Mgpyiy6ObJ5W8CAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAoSc6Skb4\ng1e0ZqPKXBV2qbx7hlqIyYpubCl1rDiEdVzqYYZEwmst36fJRRrVaFuAM/1DYAmT\nWMhU+yTfA+vCS4tql9b9zUhPw/IDHpBDWyR01spoZFBF/hE1MGNpCSXXsAbmCiVf\naxrIgR2DNketbDxkQx671KwF1+1JOMo9ffXp+OhuRo5NaGIxhTsZ+f/MA4y084Aj\nDI39av50sTRTWWShlN+J7PtdQVA5SZD97oYbeUeL7gI18kAJww9eUdmT0nEjcwKs\nxsQT1fyKbo7AlZBY4KSlUMuGnn0VnAsB9b+LxtXlDfnjyM8bVQx1uAfRo0DO8p/5\n3J5DTjAU55deBQ==\n-----END CERTIFICATE-----\n'
};
const spdyH2Listener = Spdy.createServer({
key: creds.key,
cert: creds.cert,
spdy: {
protocols: ['h2']
}
});
const server = new Hapi.Server();
server.connection({ listener: spdyH2Listener, tls: true });
server.register([Vision, Underdog], (err) => {
if (err) {
throw err;
}
server.views({
engines: { html: Handlebars },
path: __dirname + '/templates'
});
server.route([
{
method: 'get',
path: '/',
handler: (request, reply) => {
const response = reply.view('index');
reply.push(response, '/push');
}
},
{
method: 'get',
path: '/push',
handler: (request, reply) => {
reply.view('push');
}
}
]);
server.start((err) => {
if (err) {
throw err;
}
const request = Http2.get({ path: '/', port: server.info.port, agent });
request.on('response', (response) => {
console.log('Response status', response.statusCode);
response.on('data', (data) => {
console.log('Response chunk', data.toString());
});
});
request.on('push', (promise) => {
console.log('Push URL', promise.url);
promise.on('response', (pushStream) => {
console.log('Push stream status', pushStream.statusCode);
pushStream.on('data', (data) => {
console.log('Push stream chunk', data.toString());
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment