-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
41 lines (34 loc) · 1.05 KB
/
app.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
require("dotenv").config();
const express = require("express");
const router = require("./routes");
const app = express();
const cors = require("cors");
const port = process.env.PORT || 8000;
const mongoose = require("mongoose");
const path = require("path");
app.use(router);
app.use(cors());
const dbURL = process.env.DB;
mongoose
.connect(dbURL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
})
.then(() => console.log("Connected to the database!"))
.catch((err) => console.log("No connection :(", err));
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();
});
}
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
app.listen(port, () => console.log(`listening to port ${port}`));