forked from jelin-mi/petbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
44 lines (36 loc) · 899 Bytes
/
main.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
const setupApp = require('./app');
const { startDB, stopDB } = require('./db');
const port = process.env.PORT || 3000;
function stopServer(server) {
server.close(error => {
if (error) {
console.error(error.message);
}
console.log('stopped http server');
});
}
async function run() {
const app = setupApp();
let httpServer;
try {
const db = await startDB();
console.log(`Connected to Mongo! Database name: "${db.connections[0].name}"`);
httpServer = app.listen(port, () => {
console.log(`Starting on port ${port}`);
});
} catch (e) {
console.log('error starting server', e);
}
async function stop() {
console.log('stop server');
try {
await stopDB();
stopServer(httpServer);
} catch (e) {
console.log('error closing db');
}
}
process.on('SIGINT', stop);
process.on('SIGTERM', stop);
}
run();