-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
93 lines (81 loc) · 2.76 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
/**
* Created by 圆环之理 on 2018/8/4.
*
* 功能:项目入口
*
*/
'use strict';
import express from 'express';
import mySqldb from './web/mySql/db.js';
import mongodb from './web/mongodb/db.js';
import config from 'config-lite';
import cookieParser from 'cookie-parser';
import session from 'express-session';
import connectMysql from 'connect-mysql'; //该模块用于将session存入mongo中
import winston from 'winston'; //日志
import expressWinston from 'express-winston'; //日志中间插件
import path from 'path';
import history from 'connect-history-api-fallback'; //就是让你的单页面路由处理更自然(比如vue-router的mode设置为html5时)参考地址:https://github.com/bripkens/connect-history-api-fallback
import Statistic from './web/middlewares/statistic';
import router from './routes';
import getData from './web/models/getData';
const {sessions, mySqlLink} = config(__dirname);
// 爬取数据
getData();
const app = express();
app.all('*', (req, res, next) => {
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS');
res.header('Access-Control-Allow-Credentials', 'true'); //可以带Cookies
res.header('X-Powered-By', '3.2.1');
if (req.method === 'OPTIONS') {
res.send(200);
} else {
next();
}
});
app.use(Statistic.apiRecord);
const MysqlStore = connectMysql(session);
app.use(cookieParser()); //cookie运用
//session运用
app.use(session({
name: sessions.name,
secret: sessions.secret,
resave: true,
saveUninitialized: false,
cookie: sessions.cookie,
store: new MysqlStore(mySqlLink) // new MysqlStore({url: defaultConfig.url})
}));
//正确日志
app.use(expressWinston.logger({
transports: [
new (winston.transports.Console)({
json: true,
colorize: true
}),
new winston.transports.File({
filename: `logs/${new Date().getFullYear()}-${new Date().getMonth() + 1}-${new Date().getDate()}-success.log` // 根据日期生成日志成功文件
})
]
}));
app.use('/', router);
//错误日志
app.use(expressWinston.errorLogger({
transports: [
new winston.transports.Console({
json: true,
colorize: true
}),
new winston.transports.File({
filename: `logs/${new Date().getFullYear()}-${new Date().getMonth() + 1}-${new Date().getDate()}-error.log` // 根据日期生成日志错误文件
})
]
}));
app.use(history());
app.use(express.static('./public'));
app.use((req, res, next) => {
res.status(404).render('404.jade');
next();
});
module.exports = app;