-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
72 lines (59 loc) · 1.73 KB
/
index.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
const express = require('express')
require('express-async-errors')
const compression = require('compression')
const noCache = require('nocache')
const requireDir = require('require-dir')
const bodyParser = require('body-parser')
const cors = require('cors')
const frameguard = require('frameguard')
const requestIp = require('request-ip')
const useragent = require('express-useragent')
const Config = require('./config')
const exception = require('./utils/exception')
const logs = require('./utils/logs')
const routes = requireDir('./controllers', { recurse: true })
const app = express()
// get PORT from .env file info
const port = process.env.PORT || '3020' // default to 3020 if port info not set
app.disable('x-powered-by')
app.use(cors())
app.use(frameguard())
app.use(noCache())
app.use(requestIp.mw())
app.use(useragent.express())
app.use(bodyParser.json({ limit: '10mb' })) // for parsing application/json
app.use(compression())
app.get('/', (req, res) => {
res.json({
meta: {
code: 200,
error: null,
message: `Server is running at: http://localhost:${port}`,
},
})
})
app.use('/todo', routes.todo)
app.use((err, req, res, next) => {
if (err) {
const [error, status] = exception(err)
logs.logWithMask(req, error.message)
if (status === 500) {
res.status(status).json({
meta: {
code: 500,
message: Config.env.PRODUCTION ? 'Please contact admin' : error.message,
},
})
} else {
res.status(status).json({ meta: error })
}
return
}
next()
})
app.use((req, res) => {
if (!res.headersSent) {
res.status(404).json({ message: 'Sorry can\'t find that!' })
}
})
app.listen(port, () => console.log(`Server listening http on ${port}`))