-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.js
100 lines (79 loc) · 2.24 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
'use strict';
//require('tingyun');
const log4js = require('log4js');
global.logger = log4js.getLogger();
global.noop = function() {};
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const expressValidator = require('express-validator');
const MongoStore = require('connect-mongo')(session);
const mongoose = require('mongoose');
const path = require('path');
const config = require('./config');
const routes = require('./routes');
const morgan = require('morgan');
const favicon = require('serve-favicon');
const middleware = require('./middlewares');
const server = express();
server.set('view engine', config.VIEW_ENGINE);
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({
extended: false
}));
server.use(expressValidator());
server.use(express.static(path.join(__dirname, 'static')));
server.use(cookieParser());
server.use(favicon(__dirname + '/static/images/favicon.ico'));
server.use(session({
secret: config.SESSION_SECRET,
resave: false,
saveUninitialized: true,
cookie: {
maxAge: config.COOKIE_MAX_AGE
},
store: new MongoStore({
mongooseConnection: mongoose.connection
})
}));
/*server.use(middleware.handleError);
server.use(middleware.handleSession);*/
server.use(function(req, res, next) {
res.xhr = req.xhr;
logger.debug(`request type is XMLHttpRequest:${res.xhr}`);
next();
});
routes(server);
server.use(morgan('dev'));
// catch 404 and forward to error handler
server.use(function(req, res, next) {
var err = new Error('Page Not Found');
err.status = 404;
next(err);
});
// 错误处理
// 开发环镜
if (server.get('env') === 'development') {
server.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
title: err.message,
message: err.message,
error: err
});
});
}
// 生产环境错误处理
server.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
server.locals.config = config;
server.listen(config.PORT, function() {
logger.info('server start success at port:' + config.PORT);
});
module.exports = server;