-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
182 lines (168 loc) · 5.29 KB
/
server.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//dependancies
const express = require("express");
const app = express();
const path = require("path");
const exphbs = require("express-handlebars");
let mongoose = require("mongoose");
const clientSessions = require("client-sessions");
//controllers
const productController = require("./controller/productController.js");
const userController = require("./controller/userController.js");
const cartController = require("./controller/cartController.js");
const pathController = require("./controller/pathwayController.js");
const chatController = require("./controller/chatController.js");
//------------------------------------------------------------------------------
//to be removed later if not being used
app.engine(
".hbs",
exphbs.engine({
extname: ".hbs",
helpers: {
ifeq: function(a, b, options) {
if (a == b) { return options.fn(this); }
},
ifmore: function(a, b, options) {
if (a > b) { return options.fn(this); }
},
navLink: function(url, options){
return '<li' +
((url == app.locals.activeRoute) ? ' class="active" ' : '') +
'><a href="' + url + '">' + options.fn(this) + '</a></li>';
},
safeHTML: function(context){
return stripJs(context);
},
formatDate: function(dateObj){
let year = dateObj.getFullYear();
let month = (dateObj.getMonth() + 1).toString();
let day = dateObj.getDate().toString();
return `${year}-${month.padStart(2, '0')}-${day.padStart(2,'0')}`;
},
ini: function(a, options){
return a[0].toUpperCase();
},
discount: function(price, discount){
return price - (price * (discount/100));
},
displayCart: function(cartName, cartID, options){
if (cartName) return cartName
else {
console.log(typeof(cartID) + " + " + cartID)
return cartID
return cartID.substr(1, cartID.length)
}
},
calcTotal: function(Products, discount){
total = 0
console.log(Products);
if(Products && Products.length !== 0){
Products.forEach(product => {
total += product.price * product.quantity
})
}
return total - (discount/100) * total
}
},
ifmore: function (a, b, options) {
if (a > b) {
return options.fn(this);
}
},
navLink: function (url, options) {
return (
"<li" +
(url == app.locals.activeRoute ? ' class="active" ' : "") +
'><a href="' +
url +
'">' +
options.fn(this) +
"</a></li>"
);
},
safeHTML: function (context) {
return stripJs(context);
},
formatDate: function (dateObj) {
let year = dateObj.getFullYear();
let month = (dateObj.getMonth() + 1).toString();
let day = dateObj.getDate().toString();
return `${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`;
},
ini: function (a, options) {
return a[0].toUpperCase();
},
// discount: function (price, discount) {
// return price - price * (discount / 100);
// },
displayCart: function (cartName, cartID, options) {
if (cartName) return cartName;
else {
console.log(typeof cartID + " + " + cartID);
return cartID;
return cartID.substr(1, cartID.length);
}
},
calcTotal: function (Products, discount) {
total = 0
if (Products) {
Products.forEach((product) => {
total += product.price * product.quantity;
});
}
return total - discount * total;
},
}
)
);
app.set("view engine", ".hbs");
app.set("layout", "main");
//express configurations
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, "/public")));
app.use(
clientSessions({
cookieName: "session",
secret: "ThisIsaSuperLongSecretKeyWithNumbers1234567890!@#$%^&*()",
duration: 60 * 60 * 1000 * 24 * 7, // 1 week
activeDuration: 1000 * 60 * 60 * 24 // 1 day
}));
app.use(function(req,res,next) {
//res.locals.userExists = req.session.user.userName;
res.locals.session = req.session;
next();
})
//-routes-------------------------------------------------------------------
const HTTP_PORT = process.env.PORT || 8080;
function onHttpStart() {
console.log("Express http server listening on: " + HTTP_PORT);
}
app.use("/", userController);
app.use("/product", productController);
app.use("/cart", cartController);
app.use("/pathway", pathController);
app.use("/chat", chatController);
app.get("/", (req, res) => {
res.render("home", { layout: "main" });
});
app.get("/noaccess", (req, res) => {
res.render("noaccess", {
layout: "main",
});
});
// for the pages that do not exist
app.use((req, res) => {
res.render("404", {
layout: "main",
});
});
mongoose
.connect(process.env.MONGO_CONNECTION_STRING, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
app.listen(HTTP_PORT, onHttpStart);
})
.catch(() => {
console.log("Error while connection to mongoDB");
});