-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
47 lines (41 loc) · 1.26 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
const express = require('express'),
logger = require('morgan'),
compression = require('compression'),
cors = require('cors'),
bodyParser = require('body-parser'),
http = require('http')
//express server app
const app = express()
app.disable('x-powered-by')
//allow using service behind the local reverse proxy (haproxy, nginx)
app.set('trust proxy', 'loopback')
//middleware
app.use(compression())
app.use(logger('dev'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))
app.use(cors())
//register router
require('./api/asset-meta-api')(app)
// error handler
app.use((err, req, res, next) => {
if (err) console.error(err)
res.status(500).end()
})
//use server port from the PORT environment variable or 3000 port by default
function normalizePort(val) {
let port = parseInt(val)
if (isNaN(port)) return val
if (port >= 0) return port
return false
}
const serverPort = normalizePort(process.env.PORT || '3000')
app.set('port', serverPort)
//create HTTP listener
const server = http.createServer(app)
server.listen(serverPort)
server.on('listening', function () {
const addr = server.address(),
bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port
console.log('Listening on ' + bind)
})