-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
93 lines (75 loc) · 2.44 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
90
91
92
93
var express = require('express');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var http = require('http');
var path = require('path');
var config = require('config');
var log = require('lib/log')(module);
var mongoose = require('lib/mongoose');
var HttpError = require('error').HttpError;
var errorHandler = require('express-error-handler');
var app = express();
app.engine('ejs', require('ejs-mate'));
app.set('views', __dirname + path.sep + 'template');
app.set('view engine', 'ejs');
app.use(require('serve-favicon')(__dirname + '/public/images/favicon.ico'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser(config.get('session:secret')));
app.use(session({
secret: config.get('session:secret'),
key: config.get('session:key'),
resave: false,
cookie: config.get('session:cookie'),
store: require('lib/sessionStore'),
saveUninitialized: false
}));
app.use(require('middleware/sendHttpError'));
app.use(require('middleware/loadUser'));
// роуты приложения
require('routes')(app);
app.use(express.static(path.join(__dirname, 'public')));
app.use(morgan("dev"));
app.use(function(req, res) {
res.redirect('/');
});
//<editor-fold desc="обработчик ошибок">
app.use(function (err, req, res, next) {
if (typeof err == 'number') {
err = new HttpError(err);
}
if (err instanceof HttpError) {
res.sendHttpError(err);
} else {
if (app.get('env') == 'development') {
errorHandler()(err, req, res, next);
} else {
log.error(err);
err = new HttpError(500);
res.sendHttpError(err);
}
}
});
//Deploy
if(process.env.NODE_ENV==="production"){
var server = http.createServer(app);
server.listen(process.env.PORT, process.env.IP || '0.0.0.0', function () {
log.info('Express server listening on port ' + process.env.PORT);
});
}
//local
else {
var server = http.createServer(app);
server.listen(3000, 'localhost', function () {
log.info('Express server listening on port ' + 3000);
});
}
var io = require('socket')(server);
require('socket/userEvents')(io);
require('socket/teamEvents')(io);
require('socket/characterEvents')(io);
require('socket/cityEvents')(io);
require('socket/battleEvents')(io);
app.set('io', io);