-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
122 lines (102 loc) · 3.25 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const cors = require("cors");
const session = require("express-session");
// const helmet = require("helmet");
// const compression = require("compression");
const MongoDBStore = require("connect-mongodb-session")(session);
const port = process.env.PORT || 5000;
const mongodbURL = `mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@cluster0.btdla2l.mongodb.net/${process.env.MONGO_DEFAULT_DATABASE}?retryWrites=true&w=majority`;
const app = express();
app.set("trust proxy", 1);
const store = new MongoDBStore({
uri: mongodbURL,
collection: "sessions",
});
const userControllers = require("./controller/userControllers");
const productControllers = require("./controller/productControllers");
const cartControllers = require("./controller/cartControllers");
app.use(
cors({
// origin: "http://localhost:3006",
origin: [
"https://e-commerce-shop-a3.netlify.app",
"https://e-commerce-admin-a3.netlify.app",
],
// origin: process.env.COR_ORIGIN.split(","),
methods: ["POST", "PUT", "GET", "OPTIONS", "HEAD"],
credentials: true,
})
);
// app.use(helmet());
// app.use(compression());
// app.use((req, res, next) => {
// res.send("Server on");
// next();
// });
app.use(
session({
secret: "my secret",
resave: false,
saveUninitialized: false,
cookie: { sameSite: "none", secure: true, maxAge: 1000 * 60 * 60 },
// cookie: { sameSite: "lax", secure: false, maxAge: 1000 * 60 * 60 },
store: store,
})
);
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false }));
// app.use(multer().array("images", 5));
app.get("/", (req, res, next) => {
console.log(req.session);
console.log("#1 ok");
res.send("hello nodejs");
});
app.get("/check-session", userControllers.checkSession);
//Sign up
app.post("/users/signup", userControllers.postSignUp);
//Sign in
app.post("/users/signin", userControllers.postSignIn);
//add-product
app.post(
"/add-product",
userControllers.checkAdmin,
productControllers.postAddProduct
);
//edit-product
app.post(
"/edit-product",
userControllers.checkAdmin,
productControllers.postEditProduct
);
//delete product
app.post(
"/delete-product",
userControllers.checkAdmin,
productControllers.postDeleteProduct
);
//Fetch all product
app.get("/products", productControllers.getProducts);
app.get("/products/pagination", productControllers.getFilteredProducts);
//Fetch product detail
app.get("/products/:productId", productControllers.getProductDetail);
app.get("/cart", cartControllers.getCart);
app.post("/cart/update", cartControllers.postUpdateCart);
app.post("/cart/delete", cartControllers.postDeleteCartItem);
app.get("/users/logout", userControllers.postLogOut);
app.post("/users/send-mail", userControllers.postSendMail);
//get user cart
app.get("/users/cart", cartControllers.getHistory);
app.get("/users/cart/:cartId", cartControllers.getDetailHistory);
//get all cart (admin only)
app.get("/admin/carts", userControllers.checkAdmin, cartControllers.getAllCart);
app.use(express.static("images"));
mongoose
.connect(mongodbURL)
.then((result) => {
app.listen(port);
})
.catch((err) => {
console.log(err);
});