-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
57 lines (46 loc) · 1.19 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
require('dotenv').config()
const express = require('express')
const morgan = require('morgan')
const responseTime = require('response-time')
const cors = require('cors')
const routeRegistrar = require('./app/actions')
const PORT = process.env.PORT || 3001
const app = express()
app.enable('trust proxy')
app.set('trust proxy', 'loopback')
app.set('etag', false) // turn off
app.use(morgan('combined'))
app.use(responseTime())
const corsMw = cors({
origin: '*',
allowedHeaders: '*',
credentials: false,
maxAge: 86400,
methods: 'GET'
})
app.use(corsMw)
app.options('*', corsMw)
routeRegistrar(app)
app.listen(PORT, 'localhost', () => {
console.log(`App listening on port ${PORT}!`)
})
app.on('error', onError)
function onError(error) {
if (error.syscall !== 'listen') {
throw error
}
var bind = typeof PORT === 'string' ? 'Pipe ' + PORT : 'Port ' + PORT
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges')
process.exit(1)
break
case 'EADDRINUSE':
console.error(bind + ' is already in use')
process.exit(1)
break
default:
throw error
}
}