I am trying to create a web service that receives data from our school database. The connection service works fine until I try to receive the photos from the datasource.
The images come in as a base64 and I am using the following code to control the post_size:
app.use(express.json({limit: "2000mb"}));
app.use(express.urlencoded({limit: "2000mb", extended: false}));
This is the largest size I have used, just to make sure that data of any size can come in. This is only for testing and once I know the actual size I can adjust it accordingly.
So I have about 700 images that need to come in that are about 1MB each in size - so the 2000mb should be more than enough.
I keep getting the following message in the datasource's logs:
Error: 413 (Request Entity Too Large)
Is there a limit of expressjs, node or JSON that I am not aware off?
Would it be better to write this is PHP and use XML as the accepting format?
I would like to stick with Express and JSON if I can...
Thanks :-)
EDIT: I forgot to mention that I am running all this inside a docker container - maybe the limit is there?
I know this works for me. Use bodyParser instead. It looks like express.json and urlencoded may be deprecated.
const bodyParser = require('body-parser')
app.use(bodyParser.json({ limit: '50mb' }))
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }))
app.use(basePath + '/', routes)
Related
I have an expressJS server receiving API requests from a React APP front-end.
Anytime I push a request with an image less than 200Kb; it goes through as expected. But when the image parameter is exceeding 200Kb I get a CORS error.
CORS error if file exceed 200Kb
My setup for CORS and bodyParser size params:
app.use(bodyParser.json({limit: "50mb"}));
app.use(bodyParser.urlencoded({limit: "50mb", extended: true, parameterLimit:50000}));
app.use(cors());
I'm stuck on this. Any help would be much appreciated ;)
I just change from
app.use(bodyParser.json({limit: "50mb"}));
to
app.use(express.json({limit: "50mb"}));
I'm not sure what's the big difference here; but that solved my issue.
Still it's really annoying that the size limit results into CORS error. Not really friendly to debug :/
I am trying to POST call using Router through express but I am getting request entity too large error, can anyone please help to solve the issue?
I want to set mb limit to my POST call payload. I have tried app.use() limit setting through body-parser but seems to get the same issue.
Thanks
The default request size is 100kb in body-parser. try this
app.use(bodyParser.json({limit: '5mb'}));
app.use(bodyParser.urlencoded({limit: '5mb', extended: true}));
make sure to add this before defining the routes
For total noobs like me be sure to either just use bodyParser.json or express.json
I had both of these in my code and so no amount of changing body parser helped because it was using express.json to handle requests.
app.use(express.json());
const bodyParser = require('body-parser');
app.use(bodyParser.json());
there is a larger thread for this over on the other link.
Error: request entity too large
I am trying to upload 5 base64 images using Postman raw text option, with application/json header set.
The server keeps returning the error mentioned.
I am using NodeJS behind Nginx reverse proxy.
I have tried all of the options mentioned in the code below, including increasing the limits up to (100M), which apparently wasn't the issue.
NodeJS (MAXIMUM_FILE_SIZE_LIMIT='500mb'):
app.use(bodyParser.urlencoded({ limit: process.env.MAXIMUM_FILE_SIZE_LIMIT, parameterLimit: 100000, extended: true }));
app.use(bodyParser.json({ limit: process.env.MAXIMUM_FILE_SIZE_LIMIT, extended: true }));
// app.use(bodyParser.text({ limit: process.env.MAXIMUM_FILE_SIZE_LIMIT, extended:true }));
app.use( bodyParser.text({type : 'application/text-enriched', limit: process.env.MAXIMUM_FILE_SIZE_LIMIT}) );
app.use( bodyParser.raw({limit: process.env.MAXIMUM_FILE_SIZE_LIMIT}) );
Nginx
Configuration inside (server, http, location):
nginx.conf,
sites-enabled/default,
sites-available/default:
client_max_body_size 100M
I want to succeed uploading the images (5), which their total size is not more than 2MB.
The solution finally was to leave the nginx's client_max_body_size 5M; only at the nginx.conf, as-well as using the following lines in my NodeJS code:
app.use(bodyParser.urlencoded({ limit: '5mb', extended: true }));
app.use(bodyParser.json({ limit: '5mb' }));
PLEASE NOTE!!!
After trying a lot of combinations the problem appeared to be unrelated to the metnioned above, at all.
While using the node command in-order to execute the server, everything worked great. But, because I wanted to keep the application alive (in-case of a server reboot), I have used forever.
Forever started the application, but apparently kept throwing the 413 error everytime, regardless of nginx's and node's settings.
Switched to PM2, which finally solved that.
Good luck!
I've been scrambling the interwebs for the past 3 days, seemingly running in circles with no solution.
We've been building the application on a locally, with create-react-app and an Express.js backend. There are no issues with uploading files (of any type) on the local instance. We've since deployed to Heroku, having separate dynos for the frontend and the backend, using a proxy. As it stands, there is no nginx.conf file that can be located. Everything says we're running nginx, but no editable config file. Despite this, when trying to upload on the Heroku deployed site, a Failed to load resource: the server responded with a status of 413 (Request Entity Too Large) error is triggered when making the POST request.
I've tried adding:
// Body Parser Middleware
app.use(bodyParser.json({ limit: "500mb" }));
app.use(
bodyParser.urlencoded({
limit: "500mb",
extended: true,
parameterLimit: 5000000
})
);
We use Multer for multipart uploads which is what this concerns (uploading files, specifically audio files)
That's as follows:
const maxSize = 50 * 1024 * 1024; //30 MB
/*
Profile Upload
*/
const upload = multer({
storage: storage,
limits: { fileSize: maxSize, fieldSize: maxSize }
});
Furthermore, in the static.json for the frontend React.js side, the following has been added:
{
"max_body_size": "50m",
}
Every solution suggested on both Stackoverflow and Github has been attempted. Are we missing something here? To simulate the error for yourself:
https://www.trakz.co/upload w/ email as tester#gmail.com and password as testpass
I fixed the problem on the backend written in Node.js and Express.js by increasing the limit of json. You can refer to this snippet of code below for your reference:
app.use(express.json({limit: '50mb'}));
app.use(express.urlencoded({limit: '50mb'}));
For Express v4.16.0+ then you use express.json else you can use bodyparser
app.use(express.bodyParser({limit: '50mb'}));
app.use(express.urlencoded({limit: '50mb'}));
My Express application is returning "Request Entity Too Large" on a file upload of only a 125kb PNG file.
I have configured the body parser middleware as such:
app.use(bodyParser.urlencoded({
limit: '5mb',
type:'*/x-www-form-urlencoded',
extended: true
}));
according to the documentation. No matter how high I set the limit, or what combination of options, I always get the same result. I am using Express 4.13.3 and body-parser 1.15.2.
What am I doing wrong?
Embarrassingly, I had accidentally pointed my route handler to the wrong Express Router instance. Once I was pointed to the correct handler, Multer picked up the multi-part POST correctly.
Interestingly, when testing with Postman, it will send an array of files, even if you only select one, which is why request.file was undefined, but request.files contained the correct value.