-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.mjs
75 lines (54 loc) · 1.45 KB
/
app.mjs
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
import express from 'express'
const app = express()
import dotenv from 'dotenv'
if (process.env.NODE_ENV !== 'production') {
dotenv.config()
}
import exphbs from 'express-handlebars'
app.use(express.urlencoded({ extended: false }))
import gymChainSession from './app-setup/app-setup-session.mjs'
app.use(gymChainSession)
// Server static files
app.use(express.static('public'))
// Middleware to check if user is logged in
app.use((req, res, next) => {
if (req.session) {
res.locals.userId = req.session.loggedUserId;
console.log("User is logged in as: " + res.locals.userId);
} else {
res.locals.userId = 'visitor';
console.log("User is not logged in");
}
next();
});
// Import routes
import routes from './routes/router.mjs'
app.use('/', routes);
//Template engine
app.engine('hbs', exphbs.engine({
extname: '.hbs',
defaultLayout: 'main',
// Custom helper function if_eq
helpers: {
if_eq: function(a, b, options) {
if (a === b) {
return options.fn(this);
} else {
return options.inverse(this);
}
},
eq: function (a, b) {
return a === b;
},
and: function (a, b) {
return a && b;
}
}
}));
app.set('view engine', 'hbs');
//Debugging middleware
app.use((err, req, res, next) => {
console.error('ERROR: ' + err.message + '\n' + err.stack);
res.render('message', {message: "Oops something went wrong!", error: true});
})
export {app as gymChainApp}