-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
94 lines (86 loc) · 2.26 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const express = require("express");
const app = express();
const PORT = process.env.PORT || 3001;
require("dotenv").config();
const path = require("path");
const cors = require("cors");
// const fileUpload = require("express-fileupload");
const database = require("./database_config");
// //database connection
// database.connect();
// //
const cookieParser = require("cookie-parser");
const session = require("express-session");
const Admin = require("./Routes/Admin");
const Users = require("./Routes/Users");
const Products = require("./Routes/Products");
const Cart = require("./Routes/Cart");
const Order = require("./Routes/Order");
const Favorites = require("./Routes/Favorites");
const Address = require("./Routes/Address");
const Reviews = require("./Routes/Reviews");
const PasswordReset = require("./Routes/PasswordReset");
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(
cors({
origin: ["http://localhost:3000", "https://ecartonline.herokuapp.com"],
methods: ["GET", "POST", "PUT", "DELETE"],
credentials: true,
})
);
app.use(cookieParser());
app.use(
session({
secret: "hello",
cookie: {},
resave: false,
saveUninitialized: true,
})
);
app.use(express.static("public"));
app.use("/api", Admin);
app.use("/api", Users);
app.use("/api", Products);
app.use("/api", Favorites);
app.use("/api", Cart);
app.use("/api", Order);
app.use("/api", Address);
app.use("/api", Reviews);
app.use("/api", PasswordReset);
//get admin
app.get("/api/admin", (req, res) => {
res.json({
isLoggedIn: req.session.admin,
});
});
//
//get User
app.get("/api/user", (req, res) => {
if (req.session.user) {
res.json({
isLoggedIn: req.session.isLoggedIn,
user: req.session.user,
});
} else {
res.send(false);
}
});
//
//serve react app page //
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "./frontend/build")));
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "./frontend/build", "index.html"));
});
} else {
app.get("*", (req, res) => {
res.send("Sorry for the inconvenience Website Under construction!!");
});
}
//serve react app page //
database.connect(() => {
app.listen(PORT, () => {
console.log("Server started");
});
});