Answer the question
In order to leave comments, you need to log in
How to make 2 middleware work together?
I needed to write a middleware to check the size of the POST body, if it is greater than some value, return an error, otherwise go further.
I wrote this function:
function middleware(len = 10) {
return (req, res, next) => {
req.len = 0;
req.on('data', function(chunk) {
req.push(chunk);
req.len += chunk.length;
if (req.len > len) {
res.write('Error');
res.end();
}
});
req.on('end', function() {
if (req.len <= len) {
next();
}
});
};
}
app.use(middleware(10))
app.post('/',(req,res)=>{res.end('It works'))
curl http://host -d var=foo
curl http://host -d var=SomeBigVar00000000000000000
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question