Skip to content

Instantly share code, notes, and snippets.

@studentIvan
Last active May 21, 2020 18:36
Show Gist options
  • Save studentIvan/6c78886c140067936ff379031fd12e14 to your computer and use it in GitHub Desktop.
Save studentIvan/6c78886c140067936ff379031fd12e14 to your computer and use it in GitHub Desktop.
Fast solution to replace the node-spdy package using with the express.js on the node v12. Development purposes only!
1. npm uninstall spdy
2. npm install http2-proxy finalhandler
3. replace the code
import spdy from 'spdy';
spdy.createServer(HTTP2_OPTIONS, app).listen(HTTP2_PORT, (error) => { ...
with
import proxy from 'http2-proxy';
import finalhandler from 'finalhandler';
const defaultWebHandler = (err, req, res) => {
if (err) {
console.error('proxy error', err);
finalhandler(req, res)(err);
}
};
/** optional code, requires import os from 'os'; */
const addresses = [];
const interfaces = os.networkInterfaces();
for (const k in interfaces) {
for (const k2 in interfaces[k]) {
const address = interfaces[k][k2];
if (address.family === 'IPv4' && !address.internal) {
addresses.push(address.address);
}
}
}
const expressServer = http.createServer(app);
expressServer.listen(0, (error) => {
if (error) {
console.error(error);
return process.exit(1);
}
const { port } = expressServer.address();
console.log(`Serving http/1.1 server on http://localhost:${ port }`);
console.log(`Serving http/1.1 server on http://${ addresses[0] }:${ port }`, '\n');
const http2server = http2.createSecureServer({ ...HTTP2_OPTIONS, allowHTTP1: true });
http2server.on('request', (req, res) => {
proxy.web(req, res, {
hostname: 'localhost',
port,
onReq: (_req, { headers }) => {
if (_req.headers.host) {
headers['x-forwarded-for'] = _req.socket.remoteAddress;
headers['x-forwarded-proto'] = _req.socket.encrypted ? 'https' : 'http';
headers['x-forwarded-host'] = _req.headers.host;
}
},
}, defaultWebHandler);
});
http2server.listen(HTTP2_PORT, void 0, (http2error) => {
if (http2error) {
console.error(http2error);
return process.exit(1);
}
console.log('Serve http/2 server on https://localhost');
console.log(`Serve http/2 server on https://${ addresses[0] }`, '\n');
console.log('Don\'t forget to allow insecure localhost chrome://flags/#allow-insecure-localhost');
console.log('Mac OS trust instructions http://www.robpeck.com/2010/10/google-chrome-mac-os-x-and-self-signed-ssl-certificates/#.XD7yJM8zaDc', '\n');
return true;
});
return true;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment