-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
50 lines (39 loc) · 1.03 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
require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
const router = require("./routes");
const port = 5000;
const cors = (req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Origin,X-Requested-With, Content-Type, Accept"
);
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PUT, PATCH, DELETE, OPTIONS"
);
next();
};
const app = express();
app.use(express.json());
app.use(cors);
app.use((req, res, next) => {
next();
});
app.use("/api", router);
if (process.env.NODE_ENV === "production") {
app.use(express.static("frontend/dist/keep"));
const path = require("path");
app.get("*", (req, res) => {
res.sendFile(
path.resolve(__dirname, "frontend", "dist", "keep", "index.html")
);
});
}
mongoose.connect(process.env.MONGO_URI).then(() => {
console.log("Connected to mongodb");
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
});