-
Notifications
You must be signed in to change notification settings - Fork 37
/
app.js
144 lines (123 loc) · 3.42 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*!
* config.js
* Copyright(c) 2012
* Author: jifeng <[email protected]>
*/
require('response-patch');
moment.lang('zh-cn');
var connect = require('connect');
var fs = require('fs');
var path = require('path');
var http = require('http');
fs.exists = fs.exists || path.exists;
var ejs = require('ejs');
ejs.open = '{%';
ejs.close = '%}';
/**
* Middlewares
*/
var config = require('./config');
var forward = require('forward');
var urlrouter = require('urlrouter');
var render = require('connect-render');
var cookieMiddleware = require('response-cookie');
var RedisStore = require('connect-mredis')(connect);
var proxy = require('./proxy');
var routes = require('./routes');
exports.create = function createServer(userMockLoginMiddleware) {
var helpers = {
version: config.version,
config: config,
env: process.env.NODE_ENV,
current_user: function (req, res) {
return req.session && req.session.user;
},
};
var app = connect(
connect.query(),
cookieMiddleware(),
render({
root: path.join(__dirname, 'views', 'blue'),
layout: 'layout',
viewExt: '.html',
cache: !config.debug,
helpers: helpers
})
);
app.use('/favicon.ico', forward(path.join(__dirname, 'assets', 'favicon.ico')));
app.use('/robots.txt', forward(path.join(__dirname, 'assets', 'robots.txt')));
//页面缓存
if (config.debug) {
app.use('/assets', connect.static(__dirname + '/assets'));
} else {
app.use('/assets', connect.static(__dirname + '/assets', { maxAge: 3600000 * 24 * 365 }));
}
//当前端有nginx的配合
app.use(function (req, res, next) {
// 设置真实客户端ip
req.user_ip = req.headers['x-real-ip'] || (req.socket && req.socket.remoteAddress);
next();
});
/**
* uid log token
*/
connect.logger.token('uid', function (req, res) {
if (req.session && req.session.user && req.session.user.userid) {
return req.session.user.userid;
}
return req.connect_uid || '-';
});
/**
* client address log token
*/
connect.logger.token('remote-addr', function (req) {
return req.user_ip;
});
connect.logger.token('date', function () {
return new Date().toString();
});
connect.logger.format('taoindex', ':remote-addr :response-time - [:date] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent" :res[content-length] :uid');
app.use(connect.bodyParser());
app.use(connect.cookieParser());
app.use(connect.session({
key: 'connect.sid',
secret: config.session_secret,
cookie: { path: '/', httpOnly: true, maxAge: 3600000 * 12 },
store: new RedisStore(config.redisOptions)
}));
//路由信息
app.use(urlrouter(routes));
/**
* Error handler
*/
app.use(function (err, req, res, next) {
err.url = err.url || req.url;
// console.log(err.stack)
res.statusCode = err.status || 500;
res.render('500', {
viewname: 'error',
viewClassName: 'error_500'
});
});
/**
* Page not found handler
*/
app.use(function (req, res, next) {
res.statusCode = 404;
res.render('404', {
viewname: 'error',
viewClassName: 'error_404',
title: '迷路了?'
});
});
app = http.createServer(app);
//等底层数据准备好后启动
app.__listen = app.listen;
app.listen = function (port, callback) {
proxy.ready(function () {
app.__listen(port, callback);
});
return this;
};
return app;
};