-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (35 loc) · 1.14 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
import dotenv from 'dotenv'
import express from 'express'
import cors from 'cors'
import routes from './app/http/routes'
import bodyParser from 'body-parser'
import ev from 'express-validation'
dotenv.config()
const app = express()
// use parse content type, ignore form-data
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
// add middleware allow cors
app.use(cors())
// define route for system
app.use(routes)
// exception validation. To do: Write func handle all exception
app.use((err, req, res, next) => {
// specific for validation errors
if (err instanceof ev.ValidationError) return res.status(err.status).json(err)
if (err.name === 'InternalOAuthError') return res.status(err.oauthError.statusCode).json(JSON.parse(err.oauthError.data))
// other type of errors, it *might* also be a Runtime Error
// example handling
if (process.env.NODE_ENV !== 'production') {
return res.status(500).send(err.stack)
} else {
return res.status(500)
}
})
// catch 404 and forward to error handler
app.use((req, res, next) => {
var err = new Error('Not Found')
err.status = 404
next(err)
})
module.exports = app