-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
37 lines (34 loc) · 980 Bytes
/
server.js
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
const http = require('http')
const send = require('send')
const path = require('path')
const fs = require('fs')
const deployed = fs.readFileSync('./deployed', 'utf8').trim().split('\n')
const handler = (request, response) => {
const url = request.url
const basename = url === '/' ? 'index.html' : path.basename(url)
if (deployed.includes(basename)) {
return send(request, basename).pipe(response)
}
response.statusCode = 404
response.end()
}
if (module.parent) {
module.exports = handler
} else {
const server = http.createServer(handler)
process
.on('SIGTERM', shutdown)
.on('SIGQUIT', shutdown)
.on('SIGINT', shutdown)
.on('uncaughtException', error => {
console.error(error)
shutdown(1)
})
function shutdown (exitCode = 0) {
server.close(() => process.exit(exitCode))
}
server.listen(process.env.PORT || 8080, () => {
const port = server.address().port
console.log(`Listening on port ${port}.`)
})
}