This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (51 loc) · 1.47 KB
/
index.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
const express = require("express");
const app = express();
const connectDB = require("./config/db");
const compression = require("compression");
const path = require("path");
const morgan = require("morgan");
const cors = require("cors");
//Connect Database
connectDB();
//adding middleware
app.use(express.json());
//Compression
const shouldCompress = (req, res) => {
if (req.headers["x-no-compression"]) {
return false;
}
return compression.filter(req, res);
};
app.use(
compression({
filter: shouldCompress,
level: 9,
}),
);
//All routes
app.use("/users", require("./routes/users"));
app.use("/auth", require("./routes/auth"));
app.use("/posts", require("./routes/posts"));
app.use("/profile", require("./routes/profile"));
app.use("/profile", require("./routes/profile_stats"));
app.use("/posts", require("./routes/postfeatures"));
app.use("/leaderboard", require("./routes/leaderboard"));
// Serve static assets in production
if (process.env.NODE_ENV === "production") {
// Set static folder
app.use(express.static("client/build", { maxAge: 8640000 }));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
} else {
app.use(
cors({
origin: "http://localhost:3000",
methods: ["POST", "PUT", "GET", "OPTIONS", "HEAD"],
credentials: true,
}),
);
}
app.use(morgan("dev"));
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server started at port ${PORT}`));