-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
125 lines (99 loc) · 3.06 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
GLOBAL.BASE_PATH = __dirname;
GLOBAL.SERVICES_PATH = __dirname + '/server/services';
var express = require('express');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var exphbs = require('express-handlebars');
var mongoose = require('mongoose');
var winston = require('winston');
var fs = require('fs');
var join = require('path').join;
var dbconnect = require(SERVICES_PATH + '/dbconnect/dbconnect');
var auth = require(SERVICES_PATH + '/auth');
var sockets = require(SERVICES_PATH + '/sockets');
var getConfig = require(SERVICES_PATH + '/getconfig/getconfig');
var port = 8081;
//============= Create server ============
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(cookieParser());
// =========== DB connect as middleware ===============
app.use(dbconnect);
//============= Session ============
var sessionMiddleware = session({
secret: 'MHsession',
saveUninitialized: true,
resave: false,
store: new MongoStore({
mongooseConnection: mongoose.connection,
autoReconnect: true,
collection: 'sessions',
stringify: true,
hash: false,
ttl: 60 * 60 * 24 * 14, // 14 days
autoRemove: 'native',
autoRemoveInterval: 10,
})
});
app.use(sessionMiddleware);
//============= Static ============
app.use(express.static(__dirname + '/client'));
//============= Template engine ============
var hbs = exphbs.create({
defaultLayout: 'default',
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
// ============== ROUTES ================
app.get("/", function(req, res, next) {
if (!req.session.uid) {
res.redirect('/login');
} else {
getConfig
.getConfig(req.session.uid)
.then(function(config) {
app.locals.config = JSON.stringify(config);
res.render('main', {layout: 'main'});
});
}
});
app.get("/login", function(req, res, next) {
if (!req.session.uid) {
res.render('login');
} else {
res.redirect('/');
}
});
app.post('/login', function(req, res, next) {
auth
.auth(req.body.login, req.body.pass)
.then(function(uid) {
if (uid) {
req.session.uid = uid;
res.redirect('/');
} else {
res.end('Пользователь не найден.');
}
}, function(err) {
res.status(500).send("Internal server error");
});
});
var server = app.listen(port);
// ============ Socket IO =========
var sio = sockets.listen(server);
sio.use(function(socket, next) {
sessionMiddleware(socket.request, socket.request.res, next);
});
sio.use(function(socket, next) {
dbconnect(socket.request, socket.request.res, next);
});
// ============= Bootstrap models ==========
fs.readdirSync(join(BASE_PATH, 'server/models')).forEach(function (file) {
if (~file.indexOf('.js')) require(join(BASE_PATH, 'server/models', file));
});
console.log('Node app started on port ' + port);