-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.js
67 lines (57 loc) · 1.73 KB
/
app.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
/**
* The application entry point
*/
require('./app-bootstrap')
const config = require('config')
const express = require('express')
const bodyParser = require('body-parser')
const _ = require('lodash')
const cors = require('cors')
const logger = require('./src/common/logger')
const HttpStatus = require('http-status-codes')
const healthCheck = require('topcoder-healthcheck-dropin')
// setup express app
const app = express()
function check () {
// Return 200 OK for health check
return true
}
app.use(cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(healthCheck.middleware([check]))
app.set('port', config.PORT)
// Register routes
require('./app-routes')(app)
// The error handler
// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => {
logger.logFullError(err, req.signature || `${req.method} ${req.url}`)
const errorResponse = {}
const status = err.isJoi ? HttpStatus.BAD_REQUEST : (err.status || err.httpStatus || HttpStatus.INTERNAL_SERVER_ERROR)
if (_.isArray(err.details)) {
if (err.isJoi) {
_.map(err.details, (e) => {
if (e.message) {
if (_.isUndefined(errorResponse.message)) {
errorResponse.message = e.message
} else {
errorResponse.message += `, ${e.message}`
}
}
})
}
}
if (_.isUndefined(errorResponse.message)) {
if (err.message && status !== HttpStatus.INTERNAL_SERVER_ERROR) {
errorResponse.message = err.message
} else {
errorResponse.message = 'Internal server error'
}
}
res.status(status).json(errorResponse)
})
app.listen(app.get('port'), () => {
logger.info(`Express server listening on port ${app.get('port')}`)
})
module.exports = app