-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
57 lines (48 loc) · 1.36 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
const express = require("express");
const path = require("path");
const mongoose = require("mongoose");
const logger = require("morgan");
const cors = require("cors");
require("dotenv").config();
const PORT = process.env.PORT || 8080;
const app = express();
app.use(cors());
// Redirect to HTTPS to avoid privacy url warning
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();
});
}
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// For API call logs in terminal
app.use(logger("dev"));
const defaultRoute = require("./routes");
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Methods",
"OPTIONS, GET, POST, PUT, PATCH, DELETE"
);
res.setHeader(
"Access-Control-Allow-Headers",
"Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"
);
next();
});
app.use("/", defaultRoute);
mongoose
.connect(process.env.MONGODB_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then((result) => {
app.listen(PORT, () => {
console.log(`Practice REST API PROD DB connected on port ${PORT}`);
});
})
.catch((err) => {
console.log(err);
});