-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
79 lines (69 loc) · 2.42 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* Central file and intial source point of backend. Establishes database connection, and redirects Express routes
* to appropriate sub-compartments for implementation.
*
* @summary Initialization of backend.
* @author Amrit Kaur Singh, Thomas Garry
*/
const createError = require("http-errors");
const express = require("express");
const path = require("path");
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
const cors = require("cors");
const logger = require("morgan");
const mongoose = require("mongoose");
const config = require("./config");
// MongoDB Connection via Mongoose
mongoose.set("useUnifiedTopology", true);
mongoose.set("useNewUrlParser", true);
mongoose.connect(config.db.uri);
mongoose.connection.once("open", async () => {
console.log("Established connection to MongoDB.");
// console.log(config.db.uri);
console.log(`Server starting at Port: ${config.app.port}`);
});
const app = express();
// Middleware
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors({ methods: ["GET", "POST", "PUT", "DELETE"] }));
// Routers
app.use("/user", require("./routes/user"));
app.use("/autoEmails", require("./routes/autoEmails"));
app.use("/email", require("./routes/email"));
app.use("/item", require("./routes/item"));
app.use("/jwt", require("./routes/jwt"));
app.use("/order", require("./routes/order"));
app.use("/menuImages", require("./routes/menuImages"));
app.use("/paypal", require("./routes/paypal"));
// production
if (config.app.env === "production") {
// link React frontend
app.use(express.static(path.join(__dirname, "client", "build")));
// any routes called that are not handled by server are forwarded to frontend
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});
// development
} else {
app.get("/", (req, res) => {
res.status(200).json({ message: "Abandon All Hope Ye Who Enter Here..." });
});
}
// catch 404 and forward to error handler
app.use((req, res, next) => {
console.error("Error caught");
next(createError(404));
});
// error handler
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.json({ message: err.message });
});
module.exports = app;