forked from aem3372/JXUSTOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
118 lines (101 loc) · 2.68 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
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
var settings = require('./settings');
var express = require('express');
var routers = require('./routers');
var http = require('http');
var path = require('path');
var partials = require('express-partials');
var session = require('express-session');
var redisStore = require('connect-redis')(session);
var app = express();
var server = http.Server(app);
var sessionStore = new redisStore();
var socket_opt = {};
var KEY = require('./routers/key');
//connect mongodb
require('./models/connect');
if (app.get('env') == 'production') {
console.log('production env.');
socket_opt = {
'browser client minification': true,
'browser client etag': true,
'browser client gzip': true,
'origins': 'acdream.info:80 115.28.76.232:80'
};
app.enable('trust proxy');
app.enable('view cache');
} else {
console.log('development env.');
}
//服务器配置
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(partials());
app.use(require('cookie-parser')(settings.cookie_secret));
app.use(require('body-parser').urlencoded({
extended: true
}));
//文件上传
app.use(require('multer')({
dest: './uploads/',
rename: function(fieldname, filename) {
return filename;
}
}));
//gzip压缩传输
app.use(require('compression')());
//express-session
app.use(session({
secret: settings.cookie_secret,
store: sessionStore,
resave: false,
saveUninitialized: true
}));
//使用静态资源服务以及设置缓存(三天)
app.use(express.static(__dirname + '/public', {
maxAge: 259200000
}));
//favicon.ico缓存(30天)
app.use(require('serve-favicon')(__dirname + '/public/favicon.ico', {
maxAge: 2592000000
}));
//控制台日志
app.use(require('morgan')('dev'));
app.use(function(req, res, next){
req.session.reload(function(){
res.locals.user = req.session.user;
res.locals.time = (new Date()).getTime();
res.locals.msg = req.session.msg;
res.locals.KEY = KEY;
res.locals.loginInfo = req.cookies.loginInfo;
if (res.locals.msg) {
req.session.msg = null;
}
next();
});
});
//routers
require('./routers')(app);
app.get('/notsupported', function(req, res){
return res.render('notsupported', {
layout: null
});
});
app.get('/404', function(req, res, nxt){
nxt();
});
app.use(function(req, res){
res.status('404');
if (req.accepts('html')) {
return res.render('404', {
layout: null
});
}
res.type('txt').send('Not found');
});
//running server
server.listen(app.get('port'), function(){
console.log("Server running at http://localhost:"+(process.env.PORT || 3000));
});
//socket
require('./socket')(require('socket.io')(server, socket_opt), sessionStore);