-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
63 lines (49 loc) · 1.57 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
const express = require('express')
const morgan = require('morgan');
const {auth_router} = require('./routers/auth_router');
const createError = require('http-errors');
const mongoose = require('mongoose');
const Transaction = require('./models/Transaction');
const app = express()
app.use(morgan('dev'))
app.use(express.json())
require('dotenv').config()
require('./controllers/login_controller')
const port = process.env.PORT || 3000
app.get('/', (req, res) => res.redirect('/api/v1'))
app.use('/api/v1', auth_router)
app.use(async (req, res, next) => {
next(createError.NotFound())
})
process.nextTick(async(req, res, next) => {
try {
await mongoose.connect(process.env.DB_CONNECTION_URI)
console.log(`DB connected successfully`)
app.listen(port, () => console.log(`app listening at port ${port}`))
} catch (error) {
console.log(error)
// res.json({status: 500, message: error.message}).status(500)
return (createError.InternalServerError(error.message))
}
})
app.use((err, req, res, next) => {
res.status(err.status || 500)
res.send({
success: "false",
message: "An error has occurred",
status: err.status || 500,
error: [{
message: err.message
}]
})
})
process.on('SIGINT', async(req, res) => {
try {
await mongoose.connection.close()
console.log('Connection to Mongo Closed Successfully')
process.exit(0)
} catch (error) {
console.log(error)
res.json({status: 500, message: error.message}).status(500)
}
})