Skip to content

NODE 1428: Adapt express and koa to use HTTP/2 #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions express/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,43 @@ const express = require('express');
*/
require('express-async-errors');
const http = require('http');
const http2 = require('spdy');
const https = require('https');
const pem = require('pem');

const app = express();
const { PORT = 3000, HOST = 'localhost', SSL, CLUSTER } = process.env;
const { PORT = 3000, HOST = 'localhost', SSL, CLUSTER, HTTP2 } = process.env;
const isHttps = SSL === '1' ? true : false;
const isHttp2 = HTTP2 === '1' ? true : false;
require('./app').setup(app);

const listener = function listener() {
const { address, port } = this.address();
const protocol = isHttps ? 'https' : 'http';
const protocol = (isHttps || isHttp2) ? 'https' : 'http';
const stop = Date.now();
/* eslint-disable no-console */
console.log(`startup time: ${stop - start}`);
console.log('Server listening on %s://%s:%d', protocol, address, port);
};

function createServer() {
/* Start Server based on protocol */
isHttps
? pem.createCertificate({ days: 1, selfSigned: true }, (err, keys) => {
if (err) {
throw err;
}
https
.createServer({ key: keys.serviceKey, cert: keys.certificate }, app)
.listen(PORT, HOST, listener);
})
: http.createServer(app).listen(PORT, HOST, listener);
if (!isHttps && !isHttp2) {
http.createServer(app).listen(PORT, HOST, listener);
} else {
pem.createCertificate({ days : 1, selfSigned : true }, (err, keys) => {
let server;
if (err) {
throw err;
}
const options = { key: keys.serviceKey, cert: keys.certificate }
if (isHttps) {
server = https.createServer(options, app);
} else if (isHttp2) {
server = http2.createServer(options, app);
}
server.listen(PORT, HOST, listener);
})
}
}

if (CLUSTER) {
Expand Down
151 changes: 151 additions & 0 deletions express/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"multer": "^1.4.1",
"newrelic": "^6.1.0",
"pem": "^1.14.2",
"spdy": "^4.0.2",
"sqlite3": "^5.0.0"
},
"devDependencies": {
Expand Down
29 changes: 24 additions & 5 deletions koa/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ const Router = process.env.LEGACY_ROUTER
: require('@koa/router');
const app = new Koa();
const router = new Router();
const http2 = require('http2');
const pem = require('pem');
const render = require('koa-ejs');
const serve = require('koa-static');
const mount = require('koa-mount');
const bodyParser = require('koa-bodyparser');
const cookieParser = require('koa-cookie');
const { navRoutes } = require('@contrast/test-bench-utils');

const { PORT = 3000, HOST = 'localhost' } = process.env;

const { PORT = 3000, HOST = 'localhost', HTTP2 } = process.env;
const isHttp2 = HTTP2 === '1' ? true : false;
// setup static file serving
app.use(mount('/assets', serve(`${__dirname}/public`)));

Expand Down Expand Up @@ -48,8 +50,25 @@ navRoutes.forEach(({ base }) => {
app.use(router.routes());
app.use(router.allowedMethods());

app.listen(PORT, HOST, function listener() {
function listener() {
const { address, port } = this.address();
const protocol = isHttp2 ? 'https' : 'http';
// eslint-disable-next-line no-console
console.log('Server listening on http://%s:%d', address, port);
});
console.log('Server listening on %s://%s:%d', protocol, address, port);
};

function createServer() {
if (!isHttp2) {
app.listen(PORT, HOST, listener);
} else {
pem.createCertificate({ days : 1, selfSigned : true }, (err, keys) => {
if (err) {
throw err;
}
const options = { key: keys.serviceKey, cert: keys.certificate }
http2.createSecureServer(options, app.callback()).listen(PORT, HOST, listener);
})
}
}

createServer();
Loading