-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (45 loc) · 1.76 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
require('dotenv').config()
const { env: {MONGODB_URL, PORT_CLI} } = process
const PORT = PORT_CLI
const path = require('path')
const { api } = require('./src/api/routes')
const express = require('express')
const { name, version } = require('./package.json')
const { cors } = require('./src/api/middlewares')
const { mongoose } = require('./src/data')
console.debug('starting server')
try {
console.debug('connecting to database')
mongoose.connect(MONGODB_URL)
.then(() => {
console.info(`connected to database ${MONGODB_URL}`)
const app = express()
app.use(cors)
app.use('/api', api)
// other
app.get('*', (req, res) => {
res.status(404).send('This is not the endpoint you\'re looking for')
})
app.listen(PORT, () => console.info(`server ${name} ${version} running on port ${PORT}`))
let interrupted = false
process.on('SIGINT', () => {
if (!interrupted) {
interrupted = true
console.debug('stopping server')
console.debug('disconnecting database')
mongoose.disconnect()
.then(() => console.info('disconnected database'))
.catch(error => console.error('could not disconnect from mongo', error))
.finally(() => {
console.info(`server ${name} ${version} stopped`)
return process.exit()
})
}
})
})
.catch(error => {
console.error('could not connect to mongo', error)
})
} catch (error) {
console.error(error)
}