-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
45 lines (40 loc) · 1.32 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
import express from "express"
import CONFIG from "./config/server.config.js"
import router from "./routes/api.routes.js"
import { server_error, not_found } from './utils/errors.js'
import { connectDB } from "./datalayer/DatabaseInstance.js"
const app = express()
connectDB().then(data => {
if (data.success) {
console.log("Connected to DB sucessfully")
app.use(CONFIG.API_URL_BASE, router)
// 404 handler
app.use(not_found)
// internal server error handler
app.use(server_error)
const SERVER = app.listen(CONFIG.PORT, (error) => {
if (!error) {
console.log(`Listening on port : ${CONFIG.PORT}`)
} else {
console.error(error);
process.exit(1);
}
});
process.on('SIGTERM', () => {
console.info("Closing the server");
SERVER.close((error) => {
if (!error) {
console.info("Server closed successfully");
process.exit(0)
} else {
console.info("Error while closing the server, exiting...");
process.exit(1);
}
})
})
} else {
console.log("Failed to connect with DB")
}
}).catch(err=>{
console.log(err)
})