-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (60 loc) · 1.89 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
64
65
66
67
68
69
70
71
//Main starting point of the application
const express = require("express");
require("dotenv").config();
//const http = require("http");
const bodyParser = require("body-parser");
const morgan = require("morgan");
const router = require("./router");
const mongoose = require("mongoose");
const app = express();
//DB Setup
const mongoUri = `${process.env.MONGO_URI}`;
const connectDb = async () => {
try {
await mongoose.connect(mongoUri, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false
});
console.log("MongoDB Connected...");
} catch (err) {
console.log(err.message);
// Exit process with failure
process.exit(1);
}
};
connectDb();
// App setup
app.use(morgan("combined"));
app.use(bodyParser.json());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, OPTIONS, PUT, PATCH, DELETE"
);
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With,authorization,content-type, Content-Type, Accept, Authorization, authorization"
);
next();
});
router(app);
// Server Setup
const port = process.env.PORT || 5000;
app.listen(port);
console.log("App is listening on port " + port);
function exitHandler(options, exitCode) {
if (options.cleanup) console.log("clean");
if (exitCode || exitCode === 0) console.log(exitCode);
if (options.exit) process.exit();
}
//do something when app is closing
process.on("exit", exitHandler.bind(null, { cleanup: true }));
//catches ctrl+c event
process.on("SIGINT", exitHandler.bind(null, { exit: true }));
// catches "kill pid" (for example: nodemon restart)
process.on("SIGUSR1", exitHandler.bind(null, { exit: true }));
process.on("SIGUSR2", exitHandler.bind(null, { exit: true }));
process.on("uncaughtException", exitHandler.bind(null, { exit: true }));