-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
54 lines (48 loc) · 1.23 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 mongoose = require("mongoose");
const app = express();
const dotenv = require("dotenv");
const cors = require("cors");
const path = require("path");
dotenv.config();
if (process.env.NODE_ENV === "local") {
app.use(
cors({
origin: "https://localhost:3000",
credentials: true,
})
);
} else {
app.use(
cors({
credentials: true,
})
);
}
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "./frontend/dist")));
app.get("*", (req, res) => {
res.sendFile(
path.resolve(__dirname, "./", "frontend", "dist", "index.html")
);
});
}
const dbConnect = async () => {
try {
console.log("NODE_ENV:", process.env.NODE_ENV);
if (process.env.NODE_ENV === "local") {
await mongoose.connect(process.env.LOCAL_DB_URI);
console.log("Local database is connected ....");
} else {
await mongoose.connect(process.env.MONGODB_URI);
console.log("Production database is connected ....");
}
} catch (error) {
console.log("Database connection fail");
}
};
dbConnect();
const PORT = process.env.PORT;
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}..`);
});