Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// 开发环境生产,在控制台打印出请求记录
// if (config.debug) app.use(logger('dev'));
// http://www.cnblogs.com/vipstone/p/4865079.html
app.use(bodyParser.json({limit: '20mb'}));
app.use(bodyParser.urlencoded({limit: '20mb', extended: true}));
app.use(cookieParser(config.cookieSecret));
// 可以支持X-Forwarded-Proto(协议代理) X-Forwarded-For(ip代理), X-Forwarded-Host(主机代理)
app.set('trust proxy', 1);
if (!config.debug) {
// [所有请求]限制每个ip,一小时最多1500次请求
app.use(rateLimit({
store: new MongoStore({
uri: config.mongodbURI,
expireTimeMs: 60 * 60 * 1000
}),
windowMs: 60 * 60 * 1000,
max: 1500,
skip: (req: any, res: any) => {
// 获取客户端请求ip
let ip;
if (req.headers['x-forwarded-for']) {
ip = req.headers['x-forwarded-for'].toString().split(",")[0];
} else {
ip = req.connection.remoteAddress;
}