-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
63 lines (51 loc) · 1.47 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
61
62
63
// PACKAGES
import express from "express";
import dotenv from "dotenv";
import morgan from "morgan";
import colors from "colors";
import path from "path";
// ROUTES IMPORT
import productRoutes from "./routes/productRoutes.js";
import userRoutes from "./routes/userRoutes.js";
import orderRoutes from "./routes/orderRoutes.js";
import { errorHandler, notFound } from "./middleware/errorMiddleware.js";
import connectDB from "./config/db.js";
const app = express();
// BODY PARSER
app.use(express.json());
dotenv.config();
// CONNECT TO 'MONGODB'
connectDB();
// MORGAN
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
// API ROUTES
app.use("/api/products", productRoutes);
app.use("/api/users", userRoutes);
app.use("/api/orders", orderRoutes);
// If environment is 'PRODUCTION', 'index.html' from
// 'FrontEnd' build folder to be loaded
const __dirname = path.resolve();
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "./frontend/build")));
app.get("*", (req, res) =>
res.sendFile(path.join(__dirname, "./frontend/build/index.html"))
);
} else {
app.get("/", (req, res) => {
res.send("API is running...");
});
}
// 404 ERROR HANDLER
app.use(notFound);
// ERROR MIDDLEWARE
app.use(errorHandler);
// SERVER - LISTEN
const PORT = process.env.PORT || 5000;
const environment = process.env.NODE_ENV;
app.listen(PORT, () =>
console.log(
`Server running in ${environment} mode on PORT ${PORT}`.yellow.bold
)
);