How to use rate-limit-mongo - 1 common examples

To help you get started, we’ve selected a few rate-limit-mongo examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github 54sword / api.xiaoduyu.com / src / index.ts View on Github external
// 开发环境生产,在控制台打印出请求记录
// 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;
			}

rate-limit-mongo

Provides a Mongo store for the express-rate-limit middleware.

MIT
Latest version published 3 years ago

Package Health Score

51 / 100
Full package analysis

Popular rate-limit-mongo functions