-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
42 lines (34 loc) · 1.06 KB
/
server.ts
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
import http from 'http'
import devIp from 'dev-ip'
import { noop } from 'lodash'
import app from './app'
import { config, Environment } from './config'
import { log } from './src/utils/log.utils'
const hostname = (config.STATIC_IP && config.ENV === Environment.Development && devIp()[0]) || undefined
app.set('port', config.PORT)
const server = http.createServer(app)
server.on('error', onError)
server.on('listening', onListening)
// TODO: Figure out why TS chooses the wrong overload here
server.listen(config.PORT, hostname, noop)
function onError(error: NodeJS.ErrnoException) {
if (error.syscall !== 'listen') {
throw error
}
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
log.error(`${config.PORT} requires elevated privileges`)
process.exit(1)
break
case 'EADDRINUSE':
log.error(`${config.PORT} is already in use`)
process.exit(1)
break
default:
throw error
}
}
function onListening() {
log.info(`Listening at: ${hostname || 'localhost'}:${config.PORT}`)
}