-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
60 lines (48 loc) · 1.26 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
55
56
57
58
59
60
require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
const connectDB = require("./config/db");
const cookieParser = require("cookie-parser");
const path = require("path");
const app = express();
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PATCH, DELETE,OPTIONS"
);
next();
});
// connect to DB
connectDB().then((err) => {
if (!err) {
listen();
} else {
console.log(err);
}
});
// init middleware
app.use(express.json({ extended: false }));
// cookieParser
app.use(cookieParser());
// routes
app.use("/api/user", require("./routes/user"));
app.use("/api/attendance", require("./routes/save"));
app.get("/test", (req, res) => {
console.log("Test Route");
return res.status(202).json({ message: "Hello World" });
});
app.get("/env", (req, res) => {
return res.status(202).json({ message: `${process.env.AllowOrigin}` });
});
// PORT
const port = process.env.PORT || 5000;
function listen() {
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});
}