-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
48 lines (38 loc) · 1.13 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
const express = require('express')
const logger = require('morgan')
const cors = require('cors')
const bearerToken = require('express-bearer-token')
const passport = require('passport')
require('./middlewares/passport')
const db = require('./models')
const authRouter = require('./routes/auth')
const indexRouter = require('./routes/index')
const usersRouter = require('./routes/users')
const app = express()
app.use(logger('dev'))
app.use(cors())
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(bearerToken())
app.use(passport.initialize())
app.use(passport.session())
app.use('/', indexRouter)
app.use('/users', usersRouter)
app.use('/auth', authRouter)
app.use((error, req, res, next) => {
console.log(error)
const status = error.statusCode || 500
const message = error.message
const data = error.data
res.status(status).json({ message, data })
})
process.on('SIGINT', async () => {
await db.sequelize.connectionManager.close().then(_ => {
console.log('Close the database connection.')
}).catch(err => {
if (err) {
return console.error(err.message)
}
})
})
module.exports = app