-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
app.js
77 lines (70 loc) · 2.09 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
import express from 'express';
import db from './mongodb/db.js';
import config from 'config-lite';
import router from './routes/index.js';
import cookieParser from 'cookie-parser'
import session from 'express-session';
import connectMongo from 'connect-mongo';
import winston from 'winston';
import expressWinston from 'express-winston';
import history from 'connect-history-api-fallback';
import chalk from 'chalk';
// import Statistic from './middlewares/statistic'
const app = express();
app.all('*', (req, res, next) => {
const { origin, Origin, referer, Referer } = req.headers;
const allowOrigin = origin || Origin || referer || Referer || '*';
res.header("Access-Control-Allow-Origin", allowOrigin);
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", 'Express');
if (req.method == 'OPTIONS') {
res.sendStatus(200);
} else {
next();
}
});
// app.use(Statistic.apiRecord)
const MongoStore = connectMongo(session);
app.use(cookieParser());
app.use(session({
name: config.session.name,
secret: config.session.secret,
resave: true,
saveUninitialized: false,
cookie: config.session.cookie,
store: new MongoStore({
url: config.url
})
}))
// app.use(expressWinston.logger({
// transports: [
// new (winston.transports.Console)({
// json: true,
// colorize: true
// }),
// new winston.transports.File({
// filename: 'logs/success.log'
// })
// ]
// }));
router(app);
// app.use(expressWinston.errorLogger({
// transports: [
// new winston.transports.Console({
// json: true,
// colorize: true
// }),
// new winston.transports.File({
// filename: 'logs/error.log'
// })
// ]
// }));
app.use(history());
app.use(express.static('./public'));
app.listen(config.port, () => {
console.log(
chalk.green(`成功监听端口:${config.port}`)
)
});