5

I'm working with an Angular front and a NodeJS API that actually run in prod with some aws services (S3 and Elastic Beanstalk).

I actually get this CORS error when I'm uploading an image that seems to be to heavy or when I upload more thant two images, each taking 200Ko.

CORS error

Of course, I have already set my headers and I shouldn't have any issue with CORS, the issue there is about some req size limitation. There is some parts of my app.js.

app.use(bodyParser.json({}));
app.use(bodyParser.urlencoded({ extended: false }));

app.use("/images", express.static(path.join("images")));

app.use((req, res, next) => {
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.setHeader(
        "Access-Control-Allow-Headers",
        "Origin, X-Requested-With, Content-type, Accept, Authorization"
    );
    res.setHeader(
        "Access-Control-Allow-Methods",
        "GET, POST, PATCH, PUT, DELETE, OPTIONS"
    );
    next();
});

THX !

Bobby
  • 101
  • 7
  • Try this. `app.use(bodyParser.urlencoded({ parameterLimit: 100000, limit: '50mb', extended: true }));`. from here -https://stackoverflow.com/questions/31967138/node-js-express-js-bodyparser-post-limit – Shubham Dixit May 12 '20 at 21:41
  • I tried with this piece of code tho and I do have the same error. Fun fact, I don't have the error 'Error: request entity too large' – Bobby May 12 '20 at 22:00

1 Answers1

11

When using a webserver like Nginx before Node, all requests go through Nginx and are validated by nginx before even reaching Node.js.

Nginx has a configuration file by default located in /etc/nginx/nginx.conf. This file has a lot of default properties for all requests and one of these is client_max_body_size. The default value for that property is 1M (1 MB), and your file is most probably crossing this limit.

Check the logs of nginx just to be sure at /var/log/nginx/error.log. You should see the following error client intended to send too large body present.

Inorder to fix this, simply put the following in your nginx.conf: client_max_body_size 10M;. You can change the limit to whatever suits you.

ruthuparna k
  • 159
  • 1
  • 6