-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
32 lines (25 loc) · 816 Bytes
/
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
const express = require('express')
const bodyParser = require('body-parser')
const ev = require('express-validation')
const router = require('../index')
const app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
router(app, __dirname, 'routes', 'middlewares')
// error handler
app.use((err, req, res, next) => {
// specific for validation errors
if (err instanceof ev.ValidationError) return res.status(err.status).json(err)
// 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)
}
})
if (require.main === module) {
app.listen(3000)
console.log('Start listening at port 3000')
}
module.exports = app