forked from daquinoaldo/https-localhost
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·51 lines (42 loc) · 1.52 KB
/
index.js
File metadata and controls
executable file
·51 lines (42 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env node
const path = require("path")
const fs = require("fs")
const http = require("http")
const https = require("http2")
const express = require("express")
const compression = require("compression")
// SSL certificate
const certOptions = {
key: fs.readFileSync(path.resolve(__dirname, "cert/server.key")),
cert: fs.readFileSync(path.resolve(__dirname, "cert/server.crt"))
}
const port = process.env.PORT ||
/* istanbul ignore next: impossible to test */ 443
// run express
const app = express()
app.server = https.createServer(certOptions, app).listen(port)
// gzip compression and minify
app.use(compression())
app.set("json spaces", 0)
// redirect http to https
/* istanbul ignore else: useless to test */
if (port === 443 || process.env.HTTP_PORT)
app.http = http.createServer((req, res) => {
res.writeHead(301, { Location: "https://" + req.headers["host"] + req.url })
res.end()
}).listen(process.env.HTTP_PORT ||
/* istanbul ignore next: impossible to test */ 80)
// serve static files, if launched as: "node index.js <static-path>"
/* istanbul ignore else: useless to test */
if (require.main === module || process.env.USE_STATIC) {
let staticPath = process.cwd()
/* istanbul ignore if: impossible to test */
if (process.argv.join().includes("serve"))
staticPath = process.argv.join().replace("sudo", "")
.replace("serve", "").replace(" ", "")
app.use(express.static(staticPath))
}
// ready
console.info("Server running on port " + port + ".")
// export as module
module.exports = app