I am using Express.js to create api and validating it through express-validator, I'm trying to send a large amount of data which consists of an array with more than 2000 objects in it. You can create the array as
const ids = Array(2000)
.fill(0)
.map((_, index) => {
return {
"A": "1",
"B": "2",
"C": "0",
"D": "MO",
"E": "1",
"F": "0",
"G": "XYZ",
"H": "000",
"I": "999"
};
});
And I'm calling the api using axios.
axios.post('/demo', {
d:ids,
});
And this is my api
app.post('/demo', async (req, res, next) => {
console.log(req.body);
const errors = validationResult(req);
console.log('---- errors', errors);
res.end('hello world');
}
);
Now when I log req.body
it shows empty object {}
. Is this because express is not able to read this array or is there anything which I'm doing wrong.
try this in your server.js or app.js before app.post
express.json() is a method inbuilt in express to recognize the incoming Request Object as a JSON Object. This method is called as a middleware in your application using the code:
app.use(express.json())
express.urlencoded()
is a method inbuilt in express to recognize the incoming Request Object as strings or arrays. This method is called as a middleware in your application using the code:
app.use(express.urlencoded());
for more explanation refer this
Also one the possibility is that the default size limit may have exceeded. So in that case you can manually set the request size limit.
app.use(express.json({limit:'2mb'}))
for more explanation refer this