在我的应用程序中,我希望能够捕获由express-rate-limit包生成的消息。这是我拥有的代码的一个例子。我希望能够用中间件捕获消息部分,这样我就可以对其进行后处理(在这种情况下,我有多种语言)。
const apiCreatingAccountLimiter = rateLimit({
windowMs: 10 * 60 * 1000, // 10 minutes
max: 10, // limit each IP to 10 requests per windowMs
message: {
limiter: true,
type: "error",
message: 'maximum_accounts'
}
});然后
router.post('/signup', apiCreatingAccountLimiter, (req, res, next) => {
// handling the post request
})对于我的其他一些API消息,我有一个类似的解决方案中间件设置:
// error processing middleware
app.use((err, req, res, next) => {
const statusCode = err.statusCode || 500;
res.status(statusCode).send({
type: 'error',
message: err.message,
fields: err.fields === '' ? '' : err.fields,
code: err.code === '' ? '' : err.code,
section: err.section === '' ? 'general' : err.section
});
});但是,当尝试从express-rate-limit包中读取消息时,它似乎根本不会通过此中间件传递。我猜这是因为它甚至在到达任何API并触发这个中间件之前就发生了。
查看通过的res部分,我可以看到有一个包含以下数据的对象:
rateLimit:
{ limit: 10,
current: 10,
remaining: 0,
resetTime: 2019-10-21T12:35:46.919Z
},但这似乎并没有传输apiCreatingAccountLimiter中设置在最顶部的消息对象。我想知道我怎么才能找到它?
有谁知道如何做到这一点吗?我不希望这些消息在前端被翻译。我需要在NodeJS服务器上进行转换。我只对中间件部分感兴趣,在那里我可以捕获消息并对其进行后处理。
发布于 2019-10-21 21:38:05
在读取source code时,您应该将处理程序选项作为一个选项来使用,而不是使用另一个中间件。
const apiCreatingAccountLimiter = rateLimit({
windowMs: 10 * 60 * 1000, // 10 minutes
max: 10, // limit each IP to 10 requests per windowMs
message: "my initial message",
handler: function(req, res /*, next*/) {
var myCustomMessage = require('anotherModuleYouWannaUse_ForExemple');
res.status(options.statusCode).send(myCustomMessage);
},
});在这里,您将找到源代码的摘录
function RateLimit(options) {
options = Object.assign(
{
windowMs: 60 * 1000, // milliseconds - how long to keep records of requests in memory
max: 5, // max number of recent connections during `window` milliseconds before sending a 429 response
message: "Too many requests, please try again later.",
statusCode: 429, // 429 status = Too Many Requests (RFC 6585)
headers: true, //Send custom rate limit header with limit and remaining
skipFailedRequests: false, // Do not count failed requests (status >= 400)
skipSuccessfulRequests: false, // Do not count successful requests (status < 400)
// allows to create custom keys (by default user IP is used)
keyGenerator: function(req /*, res*/) {
return req.ip;
},
skip: function(/*req, res*/) {
return false;
},
handler: function(req, res /*, next*/) {
res.status(options.statusCode).send(options.message);
},
onLimitReached: function(/*req, res, optionsUsed*/) {}
},
options
);发布于 2020-02-25 15:27:01
我发现只有当我将statusCode设置为200时,我的前端才能捕捉到消息,尽管从技术上讲,它应该是429。因此,试着这样做:
const apiCreatingAccountLimiter = rateLimit({
windowMs: 10 * 60 * 1000, // 10 minutes
max: 10, // limit each IP to 10 requests per windowMs,
statusCode: 200,
message: {
status: 429, // optional, of course
limiter: true,
type: "error",
message: 'maximum_accounts'
}
});我尽了最大的努力来匹配你已经拥有的东西。就我个人而言,我的基本上是这样的:
const loginRatelimiter = rateLimit({
windowMs: 6 * 60 * 1000,
max: 10,
statusCode: 200,
message: {
status: 429,
error: 'You are doing that too much. Please try again in 10 minutes.'
}
})然后,在我的前端,我只是在响应传入时检查res.data.error,如果存在,则将其显示给用户。
https://stackoverflow.com/questions/58486616
复制相似问题