-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
54 lines (41 loc) · 1.2 KB
/
server.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
const express = require("express");
const dotenv = require("dotenv");
const morgan = require("morgan");
const colors = require("colors");
const cors = require("cors");
const connectDB = require("./config/db");
const { initCron } = require("./utils/cron");
dotenv.config({ path: "./config/config.env" });
// Connect to DB
connectDB();
// Routes
const auth = require("./routes/auth");
const predict = require("./routes/predict");
const app = express();
app.use(express.json());
const corsOptions = {
origin: "http://localhost:3000",
optionsSuccessStatus: 200, // some legacy browsers (IE11, various SmartTVs) choke on 204
};
// Enable CORS
app.use(cors(corsOptions));
process.env.NODE_ENV === "development" && app.use(morgan("dev"));
// Mount Routes
app.use("/auth", auth);
app.use("/predict", predict);
const PORT = process.env.PORT || 5000;
const server = app.listen(
PORT,
console.log(
`Server running on ${process.env.NODE_ENV} mode on port ${PORT}`.yellow.bold
)
);
// Handle unhandled promise rejections
process.on("unhandledRejection", (err, promise) => {
console.log(`Error: ${err.message}`.red);
//Close server and exit process
server.close(() => {
process.exit(1);
});
});
initCron();