-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
123 lines (101 loc) · 4.02 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
require("dotenv").config();
const express = require("express");
const app = express();
/*======================= Connecting to the database ===============================*/
const mongoose = require("mongoose");
const MONGODBURL = process.env.MONGODBURL;
const PORT = process.env.PORT || 5050;
mongoose
.connect(MONGODBURL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("Database connected successfully");
app.listen(PORT, () => {
console.log("Server is running on PORT 5050");
});
})
.catch((error) => {
console.error("Error connecting to database", error);
});
/*======================= Setting the view engine and the public folder ===============================*/
const path = require("path");
const ejsmate = require("ejs-mate");
app.engine("ejs", ejsmate);
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "/views"));
// Middleware for the public folder
app.use(express.static(path.join(__dirname, "/public")));
// Middleware for the form data
app.use(express.urlencoded({ extended: true }));
/*======================= Middleware for the method override ========================*/
const methodOverride = require("method-override");
app.use(methodOverride("_method"));
/*======================= Setting the express-session and flash message ===============================*/
const flash = require("connect-flash");
const session = require("express-session");
const SECRET = process.env.SECRET;
const configSession = {
name: "E-commerce",
secret: SECRET,
resave: false,
saveUninitialized: true,
cookie: {
httpOnly: true,
expires: Date.now() + 7 * 24 * 60 * 60 * 1000,
maxAge: 7 * 24 * 60 * 60 * 1000,
},
};
// Middleware for the express-session
app.use(session(configSession));
// Middleware for the flash message or popup
app.use(flash());
/*======================= Passport used for authorization and authentication ===============================*/
const passport = require("passport");
const LocalStrategy = require("passport-local");
const User = require("./models/User");
// Middleware for the passport
app.use(passport.initialize());
// Middleware for the passport session
app.use(passport.session());
// Serialize the user
passport.serializeUser(User.serializeUser());
// Deserialize the user
passport.deserializeUser(User.deserializeUser());
// Passport configuration
passport.use(new LocalStrategy(User.authenticate()));
// Custom middleware for storing the flash message or popup and the current User - increase reusability of the flash mesaage
app.use((req, res, next) => {
res.locals.currentUser = req.user;
res.locals.success = req.flash("success");
res.locals.error = req.flash("error");
next();
});
/*========================= Seeding the database only once =============================*/
const seedDB = require("./seed");
// seedDB();
/*======================= Middleware for the body-parser - used in Stripe Payment integration ===============================*/
// const bodyParser = require("body-parser");
// app.use(bodyParser.json());
// app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.json());
/*======================= Routes required for the application ===============================*/
const productRoute = require("./routes/product");
const reviewRoute = require("./routes/reviews");
const authRoute = require("./routes/auth");
const cartRoute = require("./routes/cart");
const defaultRoute = require("./routes/default");
const paymentRoute = require("./routes/payment");
// Middleware for products-routes to render on each incoming request
app.use(defaultRoute);
// Middleware for products-routes to render on each incoming request
app.use(productRoute);
// Middleware for reviews-routes to render on each incoming request
app.use(reviewRoute);
// Middleware for auth-routes to render on each incoming request
app.use(authRoute);
// Middleware for cart-routes to render on each incoming request
app.use(cartRoute);
// Middleware for payment-routes to render on each incoming request
app.use(paymentRoute);