app.js
1.67 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
/**
* 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 {SqlHelper} = require('./utils');
const bodyParser = require('body-parser');
const compression = require('compression');
const cookieParser = require('cookie-parser');
const cookieSession = require('cookie-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(cookieSession({
name: 'yoho_activity',
secret: 'activity@yoho',
maxAge: 24 * 60 * 60 * 1000
}));
app.use(compression());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(global.yoho.httpCtx());
app.use((req, res, next) => {
req.user = {}; // 全局的用户数据
req.yoho = {}; // req和res绑定yoho对象,用于传递全局数据
next();
});
const middleware = require('./middleware');
try {
// 允许跨域
app.use(cors({
credentials: true,
origin: 'http://localhost:63342'
}));
// 用户信息
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!');
});