AI.Demo/server/middleware/rateLimit.js

30 lines
706 B
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import rateLimit from 'express-rate-limit';
/**
* 登录接口速率限制5 次/分钟/IP
*/
export const loginLimiter = rateLimit({
windowMs: 60 * 1000, // 1 分钟
max: 5,
message: { error: '请求过于频繁,请稍后再试' },
standardHeaders: true,
legacyHeaders: false,
keyGenerator: (req) => {
return req.ip || req.connection.remoteAddress;
}
});
/**
* API 通用速率限制100 次/分钟/IP
*/
export const apiLimiter = rateLimit({
windowMs: 60 * 1000,
max: 100,
message: { error: '请求过于频繁,请稍后再试' },
standardHeaders: true,
legacyHeaders: false,
keyGenerator: (req) => {
return req.ip || req.connection.remoteAddress;
}
});