-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
94 lines (81 loc) · 2.23 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
const express = require("express");
const app = express();
const path = require("path");
const flash = require("connect-flash");
const paypal = require("paypal-rest-sdk");
const dotenv = require("dotenv").config();
const session = require("express-session");
const SessionStore = require("connect-mongodb-session")(session);
// Import Routes
const homeRouter = require("./routes/home.route");
const productRouter = require("./routes/product.route");
const authRouter = require("./routes/auth.route");
const cartRouter = require("./routes/cart.route");
const adminRouter = require("./routes/admin.route");
// Set template engine to ejs
app.set("view engine", "ejs");
app.set("views", "views");
app.use(flash());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, "assets")));
app.use(express.static(path.join(__dirname, "images")));
// Setup session
const STORE = new SessionStore({
uri: process.env.DB_URL,
collection: "sessions",
});
app.use(
session({
secret: "3bdelrazek Ecommerce website Session",
saveUninitialized: false,
store: STORE,
resave: true,
})
);
// Setup paypal sandbox
paypal.configure({
mode: "sandbox",
client_id: process.env.PAYPAL_CLIENT_ID,
client_secret: process.env.PAYPAL_CLIENT_SECRET,
});
// Routes Middlewares
app.use(authRouter);
app.use("/", homeRouter);
app.use("/product", productRouter);
app.use("/cart", cartRouter);
app.use("/admin", adminRouter);
app.get("/", (req, res, next) => {
res.render("index");
});
app.get("/error", (req, res, next) => {
res.status(500);
res.render("error", {
isUser: req.session.userId,
isAdmin: req.session.isAdmin,
pageTitle: "Error",
});
});
app.use((error, req, res, next) => {
res.redirect("/error");
});
app.get("/notAdmin", (req, res, next) => {
res.status(403);
res.render("notAdmin", {
isUser: req.session.userId,
isAdmin: false,
pageTitle: "Error",
});
});
app.use((req, res, next) => {
res.status(404);
res.render("notFound", {
isUser: req.session.userId,
isAdmin: req.session.isAdmin,
pageTitle: "Not Found",
});
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server is listen on port : ${port}`);
});