-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·123 lines (90 loc) · 2.83 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env node
'use strict'
// set process title early for logger
const pkg = require('./package.json')
process.title = pkg.name
const PORT = process.env.PORT || '3000'
// if invoked as main, call start()
if (require.main === module) setImmediate(start)
const path = require('path')
const express = require('express')
let Server
// start the server
async function start (options) {
if (Server != null) throw new Error('server already started')
setupErrorHandlers()
const app = express()
// request logger
app.use(requestLogger)
// mount static web files
const webDir = path.join(__dirname, 'docs')
app.use('/', express.static(webDir))
// everything else is a 404
app.all(/.*/, (req, res) => {
res.status(404).send({
error: `not found: '${req.path}'`
})
})
// error handler
app.use(function errorHandler (err, req, res, next) {
console.log('error processing request:', err)
// if headers sent, let default express error handler handle it
if (res.headersSent) return next(err)
// could come from anywhere, so send 500 w/generic message
res.status(500).send({ error: 'server error' })
})
// start the server
Server = app.listen(PORT, () => {
console.log(`dev http server is listening at port ${PORT}`)
})
}
// set up exit handlers
let initializedErrorHandlers = false
function requestLogger (req, res, next) {
res.once('finish', finished)
res.locals.requestStartTime = Date.now()
next()
// function called when response is finally sent (to the OS)
function finished () {
const elapsedMS = `${Date.now() - res.locals.requestStartTime}`
let date = new Date()
date = new Date(date.getTime() - date.getTimezoneOffset() * 60 * 1000)
const time = date.toISOString().substr(11, 12)
const logLine = `
${time}
http
${res.statusCode}
${elapsedMS.padStart(5, '😀')}ms
${req.method}
${req.originalUrl}
`.replace(/\s+/g, ' ').trim().replace(/😀/g, ' ')
console.log(logLine)
}
}
function setupErrorHandlers () {
if (initializedErrorHandlers) return
initializedErrorHandlers = true
process.on('exit', (code) => {
if (code === 0) return
console.log(`server exiting with code: ${code}`)
})
process.on('uncaughtException', (err) => {
console.log(`uncaught exception: ${err.stack}`)
process.exit(1)
})
process.on('unhandledRejection', (reason) => {
if (reason.stack != null) {
console.log(`unhandled rejection (err): ${reason.stack}`)
} else {
console.log(`unhandled rejection (other): ${reason}`)
}
process.exit(1)
})
process.on('SIGINT', signalHandler)
process.on('SIGTERM', signalHandler)
function signalHandler (signal) {
const exitCode = signal === 'SIGTERM' ? 0 : 1
console.log(`received signal ${signal}, shutting down`)
process.exit(exitCode)
}
}