-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
89 lines (76 loc) · 2.35 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
import express from "express";
import session from 'express-session';
import { fileURLToPath } from 'url';
import exphbs from 'express-handlebars';
import cookieParser from 'cookie-parser';
import { dirname } from 'path';
import configRoutes from './routes/index.js';
const app = express();
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
app.use(cookieParser());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const staticDir = express.static(__dirname + '/public');
app.use('/public', staticDir);
app.use('/', staticDir);
app.use(express.static('public'));
const hbs = exphbs.create({
defaultLayout: 'main',
helpers: {
if_eq: function (val1, val2) {
return val1 === val2;
},
not_past_date: function (date) {
const eventDate = Date.parse(date);
const now = Date.now();
return eventDate >= now;
},
generateStarRating: function(rating) {
let html = '';
for(let i = 1; i <= 5; i++) {
if (i <= rating) {
html += '<span class="filled-star">★</span>';
} else {
html += '<span class="empty-star">☆</span>';
}
}
return html;
}
}
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.use((req, res, next) => {
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
res.setHeader("Pragma", "no-cache");
res.setHeader("Expires", "0");
next();
});
app.use(session({
name: 'AuthCookie',
secret: 'some secretss',
saveUninitialized: true,
resave: false
}));
const anthenticate = (req, res, next) => {
if (!req.session.user) {
return res.redirect('/login');
}
next();
};
app.use('/logout', anthenticate);
app.use('/sendMessage', anthenticate);
app.use('/checkPassword', anthenticate);
app.use('/admin', anthenticate);
app.use('/review', anthenticate);
app.use('/user', anthenticate);
app.use('/drink', anthenticate)
configRoutes(app);
app.use('*', (req, res) => {
res.render('Error', { title: 'Page No Found', errorMsg:"wrong route!" });
});
app.listen(3000, () => {
console.log("We've now got a server!");
console.log("Server is running on http://localhost:3000");
});