-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
85 lines (69 loc) · 2.28 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
const express = require('express'),
handlebars = require("express-handlebars"),
session = require('express-session'),
compression = require('compression'),
cookieParser = require('cookie-parser');
global.common = require('./common.js');
const controller = require('./controller.js');
const app = express();
const hbs = handlebars.create({
extname: 'hbs',
helpers: {
i18n: common.i18n.translate
}
});
// initialize body-parser to parse incoming parameters requests to req.body
app.use(express.urlencoded({
extended: true
}));
// initialize cookie-parser to allow us access the cookies stored in the browser.
app.use(cookieParser());
app.use(compression());
// initialize express-session to allow us track the logged-in user across sessions.
app.use(session({
key: 'user_sid',
secret: 'somerandonstuffs',
resave: false,
saveUninitialized: false,
cookie: {
expires: 600000
}
}));
// This middleware will check if user's cookie is still saved in browser and user is not set, then automatically log the user out.
// This usually happens when you stop your express server after login, your cookie still remains saved in the browser.
app.use((req, res, next) => {
if (req.cookies.user_sid && !req.session.user) res.clearCookie('user_sid');
next();
});
app.engine('hbs', hbs.engine);
app.set('views', './views');
app.set('view engine', 'hbs');
app.use('/favicons', express.static('public/favicons_green'));
app.use('/components', express.static('components'));
app.use('/fonts', express.static('public/fonts'));
app.use('/modules', express.static('public/modules'));
app.use('/theme', express.static('public/theme'));
// route for Home-Page
app.route('/').get((req, res) => {
if (req.session.user && req.cookies.user_sid) {
res.render('login', sources);
} else {
res.render('login', sources);
}
});
app.route('/controller').post(controller);
// route for user logout
app.get('/logout', (req, res) => {
if (req.session.user && req.cookies.user_sid) {
res.clearCookie('user_sid');
res.redirect('/');
} else {
res.redirect('/login');
}
});
// route for handling 404 requests(unavailable routes)
app.use(function(req, res, next) {
res.status(404).send("Sorry can't find that!");
});
const port = process.env.PORT || 3000;
const http = app.listen(port, () => log(`App listening on port ${port}`));