-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
67 lines (55 loc) · 1.71 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
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');
app.use('/public', static);
app.use(express.json());
app.use(express.urlencoded({extended: true}));
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));
}
},
partialsDir: ['views/partials/']
});
app.engine('handlebars', handlebarsInstance.engine);
app.set('view engine', 'handlebars');
app.use(
session({
name: 'AuthCookie',
secret: "This is a secret.. shhh don't tell anyone",
saveUninitialized: false,
resave: false,
pageNow: '/',
pageBefore: '',
cookie: {maxAge: 365 * 24 * 60 * 60 * 1000}
})
);
app.use (function (req, res, next) {
if (!req.session.pageNow){
req.session.pageNow = '/';
req.session.pageBefore = '';
}
next();
});
app.use('/login', (req, res, next) => {
if (req.session.user) {
res.redirect('/profile');
} else {
//here I',m just manually setting the req.method to post since it's usually coming from a form
//req.method = 'POST'; // if it req.method = 'POST', it will add route as: get login: http://localhost:3000/login
}
next();
});
configRoutes(app);
app.listen(3000, () => {
console.log("We've now got a server!");
console.log('Your routes will be running on http://localhost:3000');
});