Skip to content

Commit da461e6

Browse files
committed
HTTP/2 and compression
1 parent f461906 commit da461e6

3 files changed

Lines changed: 168 additions & 4 deletions

File tree

package-lock.json

Lines changed: 152 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "HTTPS server running on localhost",
55
"main": "server.js",
66
"scripts": {
7-
"start": "node server.js"
7+
"start": "node server.js",
8+
"http2": "HTTP2=true node server.js"
89
},
910
"repository": {
1011
"type": "git",
@@ -17,6 +18,8 @@
1718
},
1819
"homepage": "https://github.com/daquinoaldo/https-localhost#readme",
1920
"dependencies": {
20-
"express": "latest"
21+
"compression": "latest",
22+
"express": "latest",
23+
"spdy": "latest"
2124
}
2225
}

server.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
const path = require('path');
22
const fs = require('fs')
3-
const https = require('https')
3+
const http = require('http');
4+
const https = process.env.HTTP2 ? require('spdy') : require('https')
45
const express = require('express')
6+
const compression = require('compression');
57

68
const certOptions = {
79
key: fs.readFileSync(path.resolve('cert/server.key')),
810
cert: fs.readFileSync(path.resolve('cert/server.crt'))
911
}
1012

1113
const app = express()
12-
https.createServer(certOptions, app).listen(443)
14+
app.use(compression());
15+
https.createServer(certOptions, app).listen(443);
16+
17+
// redirect http to https
18+
http.createServer(function (req, res) {
19+
res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url });
20+
res.end();
21+
}).listen(80);

0 commit comments

Comments
 (0)