-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.js
More file actions
93 lines (83 loc) · 2.51 KB
/
Copy pathapp.js
File metadata and controls
93 lines (83 loc) · 2.51 KB
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
const express = require("express");
const app = express();
const static = express.static(__dirname + "/public");
const session = require("express-session");
const configRoutes = require("./routes");
const exphbs = require("express-handlebars");
const Handlebars = require("handlebars");
// const handlebarsInstance = exphbs.create({
// defaultLayout: "main",
// // Specify helpers which are only registered on this instance.
// helpers: {
// asJSON: (obj, spacing) => {
// if (typeof spacing === "number")
// return new Handlebars.SafeString(JSON.stringify(obj, null, spacing));
// return new Handlebars.SafeString(JSON.stringify(obj));
// },
// checkListMem: function (elem, target) {
// if (elem && target) {
// return target.includes(elem);
// } else return false;
// },
// checkListLengthZero: function (arr) {
// return arr.length == 0;
// },
// },
// partialsDir: ["views/partials/"],
// });
app.use("/public", static);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.engine("handlebars", exphbs({ defaultLayout: "main" }));
// app.engine("handlebars", handlebarsInstance.engine);
app.set("view engine", "handlebars");
app.use(
session({
name: "AuthCookie",
secret: "Some secret",
saveUninitialized: true,
resave: false,
cartItems: [],
})
);
app.use("/users/details", async (req, res, next) => {
if (req.session.user) {
// TODO: need to implement error file to reDirect or render
next();
} else {
return res.render("pages/loginPage", { title: "first time" });
}
});
app.use((req, res, next) => {
date = new Date();
console.log(
`[${date.toUTCString()}]:\t${req.method}\t${req.originalUrl}\t\t${
req.session.user ? "(Authenticated)" : "(Not Authenticated)"
}`
);
next();
});
app.use((req, res, next) => {
date = new Date();
console.log(
`[${date.toUTCString()}]:\t${req.method}\t${req.originalUrl}\t\t${
req.session.admin ? "(Admin Authenticated)" : "(Admin Not Authenticated)"
}`
);
next();
});
// // buy button middleware
// //if logged in will add to cart , else go to login page.
// app.use("/addToCart", async (req, res, next) => {
// if (!req.session.user) {
// return res.redirect("/login");
// } else {
// //buy button should trigger
// return res.redirect("/");
// }
// });
configRoutes(app);
app.listen(3000, () => {
console.log("We've now got a server!");
console.log("Your routes will be running on http://localhost:3000");
});