-
Notifications
You must be signed in to change notification settings - Fork 38
/
app.js
71 lines (62 loc) · 1.66 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
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
/**
* @class
* Library imports
*/
const express = require("express");
const path = require("path");
const cors = require("cors");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
/**
* Router imports
*/
const productRouter = require("./routes/products");
const paymentRouter = require("./routes/payment");
const checkoutRouter = require("./routes/checkout");
const utilRouter = require("./routes/util");
// Initialize the app and port
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const port = process.env.PORT || 6000;
if (process.env.NODE_ENV !== "production") {
const config = require("dotenv").config();
}
/**
* Connecting to Mongoose database
*/
mongoose.connect(
process.env.mongooseConnection,
{ useNewUrlParser: true, useUnifiedTopology: true },
function (err, res) {
if (err) {
console.log("ERROR connecting to mongoDB", err);
} else {
console.log("Succeeded connecting to mongoDB!");
}
}
);
/**
* Router for handling backend endpoint requests
*/
app.use("/api/products", productRouter);
app.use("/api/payment", paymentRouter);
app.use("/api/checkout", checkoutRouter);
app.use("/util", utilRouter);
/**
* Production dependency for frontend connection
*/
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "client/build")));
app.get("*", function (req, res) {
res.sendFile(path.join(__dirname, "client/build", "index.html"));
});
}
/**
* Finally listening to the port
*/
app.listen(port, (error) => {
if (error) throw error;
console.log("Server running on port " + port);
});