-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
52 lines (39 loc) · 1.24 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
const express = require("express");
var cookieParser = require("cookie-parser");
require("dotenv").config({ path: "./config/.env" });
require("./config/DBConnection");
const useRouter = require("./routes/router");
const app = express();
//app uses
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use("/api", useRouter);
const port = process.env.PORT || 5000;
if (process.env.NODE_ENV == "production") {
app.use((req, res, next) => {
if (req.header("x-forwarded-proto") !== "https") {
res.redirect(`https://${req.header("host")}${req.url}`);
} else {
next();
}
});
const path = require("path");
app.use(
"/static",
express.static(path.join(__dirname, "client", "build/static"))
);
app.use(
"/manifest.json",
express.static(path.join(__dirname, "client", "build", "manifest.json"))
);
app.use(
"/favicon.ico",
express.static(path.join(__dirname, "client", "build", "favicon.ico"))
);
app.use(express.static(path.join(__dirname, "../client/build")));
app.get("/*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
app.listen(port, () => console.log(`Server is running at ${port}`));