-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
65 lines (55 loc) · 1.71 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
const app = require('express')()
const bodyParser = require('body-parser')
const version = process.env.CURRENT_VERSION || "v3"
// Middlewares
app.use(require('helmet')()) // security headers
app.use(require('compression')()) // compression
app.use(require('morgan')('dev')) // logging
app.use(require('cors')()) // CORS
app.use(require('cookie-parser')(process.env.COOKIES_SECRET || 'Yum!')) // 🍪
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(bodyParser.text())
app.use((req, res, next) => {
req.args = {}
Array('params', 'query', 'body').forEach(reqKey => {
if (typeof req[reqKey] === 'object') {
Object.assign(req.args, req[reqKey])
}
});
next()
})
// Routes
app.get('/', (req, res) => {
res.send('Somewhere, something incredible is waiting to be known. <br> - Carl Sagan')
})
app.get('/health-check', (req, res) => res.sendStatus(200))
app.use('/v2', require('./v2/router'))
app.use('/v3', require('./v3/router'))
app.use('/', require(`./${version}/router`))
// 404 error handling
app.use((req, res, next) => {
next({ status: 404, response: 'Not Found' })
})
// error handling
app.use((error, req, res, next) => {
const dev = process.env.NODE_ENV !== 'production'
console.error(`ERR ${req.originalUrl} - ${error.message || error.response}`)
// istanbul ignore next
if (error.stack) console.error(` ${error.stack}`)
if (error.isJoi) {
delete error.isJoi
return res.status(403).json({
success: false,
error: error
})
}
// istanbul ignore next
res.status(error.status || 500).json({
error: {
message: error.response || 'Something went wrong!',
error: dev ? error : undefined
}
})
})
module.exports = app