-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
95 lines (81 loc) · 2.53 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
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
/**
* The application entry point
*/
require('./src/bootstrap')
const _ = require('lodash')
const config = require('config')
const cors = require('cors')
const bodyParser = require('body-parser')
const express = require('express')
const httpStatus = require('http-status-codes')
const path = require('path')
const helper = require('./src/common/helper')
const logger = require('./src/common/logger')
const routes = require('./src/routes')
const app = express()
app.set('port', config.PORT)
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(cors({ credentials: true, origin: true }))
app.use(express.static(path.join(__dirname, 'src/templates')))
const apiRouter = express.Router()
_.each(routes, (verbs, url) => {
_.each(verbs, (def, verb) => {
let actions = [
(req, res, next) => {
req.signature = `${def.controller}#${def.method}`
next()
}
]
const method = require(`./src/controllers/${def.controller}`)[def.method] // eslint-disable-line
if (!method) {
throw new Error(
`${def.method} is undefined, for controller ${def.controller}`
)
}
if (def.middleware && def.middleware.length > 0) {
actions = actions.concat(def.middleware)
}
actions.push(method)
logger.info(`API : ${verb.toLocaleUpperCase()} ${config.API_PREFIX}${url}`)
apiRouter[verb](
`${config.API_PREFIX}${url}`,
helper.autoWrapExpress(actions)
)
})
})
app.use('/', apiRouter)
// Error handling
app.use((err, req, res, next) => {
// eslint-disable-line no-unused-vars
if (err.isJoi) {
res.status(httpStatus.BAD_REQUEST).json({
message: err.details[0].message
})
} else if (err.errors) {
res.status(httpStatus.BAD_REQUEST).json({ message: err.errors })
} else {
const statusCode = err.statusCode || httpStatus.INTERNAL_SERVER_ERROR
res
.status(statusCode)
.json({ message: err.message || config.DEFAULT_MESSAGE })
}
})
// Check if the route is not found or HTTP method is not supported
app.use('*', (req, res) => {
const pathKey = req.baseUrl.replace(config.API_PREFIX, '')
const route = routes[pathKey]
if (route) {
res
.status(httpStatus.METHOD_NOT_ALLOWED)
.json({ message: 'The requested HTTP method is not supported.' })
} else {
res
.status(httpStatus.NOT_FOUND)
.json({ message: 'The requested resource cannot be found.' })
}
})
app.listen(app.get('port'), () => {
logger.debug(`Express server listening on port ${app.get('port')}`)
})
module.exports = app