-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
96 lines (85 loc) · 2.89 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
const express = require("express");
const path = require("path");
const app = express();
const dotenv = require("dotenv").config();
const port = process.env.PORT || 4000;
const mongoose = require("mongoose");
const config = require("./config/database");
const bodyParser = require("body-parser");
const session = require("express-session");
const cookie = require("cookie-parser");
const flash = require("connect-flash");
const nocache = require("nocache");
const userRouter = require("./routes/user-router");
const adminRouter = require("./routes/admin-router");
const categoryRouter = require("./routes/category-router");
const couponRouter = require("./routes/coupon-router");
const productRouter = require("./routes/product-router");
const profileRouter = require("./routes/profile-router");
const userProductRouter = require("./routes/user-product-router");
const cartRouter = require("./routes/cart-router");
const wishlistRouter = require("./routes/wishlist-router");
const orderRouter = require("./routes/order-router");
const Wishlist = require("./models/wishlistModel");
const Cart = require("./models/cartModel");
const orderStatusRouter = require("./routes/order-status-router");
mongoose
.connect(config.database)
.then(() => {
console.log("Database Connected");
})
.catch((err) => {
console.log("Database connection failed" + err);
});
app.set("view engine", "ejs");
app.use(nocache());
app.use(cookie("cookieSecret"));
app.use(
session({
secret: "sessionSecret",
resave: true,
saveUninitialized: true,
cookie: { maxAge: 60 * 1000 * 60, secure: false },
})
);
app.use("/views", express.static(path.join(__dirname, "views")));
app.use("/public", express.static(path.join(__dirname, "public")));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(flash());
app.use("/", userRouter);
app.use("/admin", adminRouter);
app.use("/admin/category", categoryRouter);
app.use("/admin/product", productRouter);
app.use("/admin/orders", orderStatusRouter);
app.use("/admin/coupon", couponRouter);
app.use("/profile", profileRouter);
app.use("/products", userProductRouter);
app.use("/cart", cartRouter);
app.use("/wishlist", wishlistRouter);
app.use("/orders", orderRouter);
app.get("/admin/*", (req, res) => {
let admin = req.session.admin;
res.render("admin/404", { admin });
});
app.get("*", async (req, res) => {
let user = req.session.user;
let count = null;
if (user) {
const cartItems = await Cart.findOne({ userId: user._id });
if (cartItems) {
count = cartItems.cart.length;
}
}
let wishcount = null;
if (user) {
const wishlistItems = await Wishlist.findOne({ userId: user._id });
if (wishlistItems) {
wishcount = wishlistItems.wishlist.length;
}
}
res.render("user/404", { user, count, wishcount });
});
app.listen(port, () => {
console.log(`Listening to the server on http://localhost:${port}`);
});