How to force to work 2 middleware together?

I needed to write middleware to check the size of the POST body if it's greater than some value, return error, otherwise move on.
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'))


Now if you run
curl http://host -d var=foo
He otbiraet It works, but if run

curl http://host -d var=SomeBigVar00000000000000000

it displays an error. Everything seems normal, but if the first `app.use` and `app.post` add `app.use(bodyParser.urlencoded());` to parse the body, this middleware does not work.
How to make it all work?
June 3rd 19 at 18:56
0 answer

Find more questions by tags JavaScriptNode.jsExpress.js