-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
125 lines (108 loc) · 3.26 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
123
124
125
const express = require("express");
const nocache = require("nocache");
const session = require("express-session");
const router = express.Router();
const path = require("path");
const app = express();
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const { v4: uuidv4 } = require("uuid");
const adminRouter = require("./routes/admin");
const userRouter = require("./routes/user");
const landingRouter = require("./routes/landing");
const PORT = process.env.PORT || 4000;
const Category = require("./models/category");
const bodyParser = require("body-parser");
const cron = require("node-cron");
const User = require("./models/users");
const {
clearCartAndMoveToWishlist,
} = require("./controller/clearCartController");
// const twilioRouter = require('./routes/twilio-sms')
// Serve static files (including app.js, sw.js, and manifest.json) from the root directory
app.use(express.static(__dirname));
const jsonParser = bodyParser.json();
app.use(jsonParser);
// app.use('/twilio-sms',twilioRouter)
// Helper function to get the category name from ID
app.locals.getCategoryName = async (categoryId) => {
try {
const category = await Category.findById(categoryId);
return category.name;
} catch (error) {
// Handle the error appropriately
}
};
dotenv.config(); // Load environment variables from .env file
app.use(nocache());
app.use(express.json());
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: false }));
// Session middleware should be defined before the router middleware
app.use(
session({
secret: uuidv4(),
resave: false,
saveUninitialized: true,
cookie: { maxAge: 600000 },
})
);
// Load static assets
app.use("/public", express.static(path.join(__dirname, "public")));
app.use("/partials", express.static(path.join(__dirname, "views/partials")));
app.use("/uploads", express.static("uploads"));
app.use("/", userRouter);
app.use("/", adminRouter);
app.use("/", landingRouter);
app.use("/admin", adminRouter);
// Database connection
mongoose.connect(process.env.DB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = mongoose.connection;
db.on("error", (error) => {});
db.once("open", () => {});
app.use((req, res, next) => {
res.locals.message = req.session.message;
delete req.session.message;
next();
});
app.use((err, req, res, next) => {
res.render("error", { error: err, request: req, title: "Error Page" });
});
app.use("*", (req, res) => {
res.render("error", { error: "", request: req, title: "Error Page" });
});
cron.schedule("*/30 * * * *", () => {
clearCartAndMoveToWishlist();
});
app.use(async (req, res, next) => {
if (req.session.cookie.expires < Date.now()) {
// Session cookie has expired
req.session.destroy((err) => {
if (err) {
return next(err);
}
const userId = req.session.user._id;
User.findOneAndUpdate(
{ _id: userId },
{ isLoggedIn: false },
(err, userId) => {
if (err) {
return next(err);
}
if (!userId) {
return res.redirect("/login.");
}
res.redirect("/login.");
}
);
});
} else {
next();
}
});
app.listen(PORT, () => {
console.log(`Server started at : http://localhost:${PORT}`);
});