1 Star 0 Fork 0

candyYan/SBG node api

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
app.js 1.82 KB
一键复制 编辑 原始数据 按行查看 历史
candy 提交于 2017-12-16 11:57 . no message
/**
* Express instance application
*/
'use strict';
import express from 'express';
import db from './mongodb/db';
import router from './routes/index';
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 path from 'path';
import history from 'connect-history-api-fallback';
import config from './config/defalut';
import Statistic from './middlewares/statistic'
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 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);
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/candyYan/SBG-node-api.git
git@gitee.com:candyYan/SBG-node-api.git
candyYan
SBG-node-api
SBG node api
master

搜索帮助