-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.js
48 lines (37 loc) · 1.29 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
const express = require("express");
const path = require("path");
const dotenv = require("dotenv");
dotenv.config({ path: "./.env" });
const connectDB = require("./config/db");
const errorHandler = require("./middleware/error");
const app = express();
app.use(express.json());
connectDB(); // Connect to databse
// API Routes
app.use("/api/auth", require("./routes/auth"));
app.use("/api/private", require("./routes/private"));
// --------------------------DEPLOYMENT------------------------------
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "./client/build")));
app.get("*", (req, res) => {
return res.sendFile(
path.resolve(__dirname, "client", "build", "index.html")
);
});
} else {
app.get("/", (req, res) => {
res.send("API is running");
});
}
// --------------------------DEPLOYMENT------------------------------
// Error Handler Middleware (Should be at the end of all middlewares)
app.use(errorHandler);
const PORT = process.env.PORT || 5000;
const server = app.listen(PORT, () =>
console.log(`Server running on PORT ${PORT}`)
);
// Handling server errors with clean error messages
process.on("unhandledRejection", (err, promise) => {
console.log(`Logged Error: ${err.message}`);
server.close(() => process.exit(1));
});