app.js
1.89 KB
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
/**
* yoho-activity-platform app
* @author: leo<qi.li@yoho.cn>
* @date: 2017/6/23
*/
'use strict';
const cors = require('cors');
const express = require('express');
const config = require('./config');
const bluebird = require('bluebird');
const ynLib = require('yoho-node-lib');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const {SqlHelper} = require('./utils');
const session = require('yoho-express-session');
const MemcachedStore = require('connect-memcached')(session);
let logger;
const app = express();
// 全局注册library
ynLib.global(config);
logger = global.yoho.logger;
global.Promise = bluebird;
global.yoho.utils = {
mysqlCli: new SqlHelper(config.mysql.database)
};
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(global.yoho.httpCtx());
app.use(session({
proxy: true,
resave: false,
saveUninitialized: true,
unset: 'destroy',
secret: 'ekwhekhwkhidfdufy222suqere999',
name: 'yoho_activity_sid',
cookie: {
httpOnly: false
},
store: new MemcachedStore({
hosts: config.memcache.session,
prefix: 'yoho_activity_sid:',
reconnect: 5000,
timeout: 1000,
retries: 0
})
}));
app.use((req, res, next) => {
req.user = {}; // 全局的用户数据
req.yoho = {}; // req和res绑定yoho对象,用于传递全局数据
next();
});
const middleware = require('./middleware');
try {
// 允许跨域
app.use(cors());
// 用户信息
app.use(middleware.user);
// 路由分发
require('./dispatch')(app);
// 404处理
app.use(middleware.error.notFound);
// 错误处理
app.use(middleware.error.serverError);
} catch (err) {
logger.error(err);
}
app.listen(config.port, function() {
logger.info('yoho-activity-platform started successfully!');
});