Skip to content

Instantly share code, notes, and snippets.

@goshmx
Last active March 23, 2026 12:24
Show Gist options
  • Select an option

  • Save goshmx/93925c75285c6726aa88 to your computer and use it in GitHub Desktop.

Select an option

Save goshmx/93925c75285c6726aa88 to your computer and use it in GitHub Desktop.
Fix Sails js error . Request entity too large. Code 413
//This fragment goes in your config/http.js file.
//Ensure to install skipper previusly...
//Tested in Sails.js v0.10.5
module.exports.http = {
bodyParser: (function () {
var opts = {limit:'50mb'};
var fn;
// Default to built-in bodyParser:
fn = require('skipper');
return fn(opts);
})
};
@IOrlandoni
Copy link
Copy Markdown

If you are seeing a http error 413 Payload Too Large error while working with Sails.js, it is almost always due to the default body parser settings. Sails uses skipper as its default body parser, which has a default limit of ~10MB.

To fix this, you need to adjust the http configuration.

1. Update config/http.js

In your Sails project, locate config/http.js. You need to explicitly set the maxBytes for the body parser inside the middleware object.

// config/http.js
module.exports.http = {
  middleware: {
    order: [
      'cookieParser',
      'session',
      'bodyParser', // Ensure bodyParser is in the order
      'handleBodyParserError',
      'compress',
      'poweredBy',
      'router',
      'www',
      'favicon',
    ],

    bodyParser: (function() {
      var opts = {
        limit: '50mb', // For JSON/URL-encoded bodies
        maxBytes: 50000000 // ~50MB for file uploads/multipart
      };
      var fn = require('skipper')(opts);
      return fn;
    })()
  }
};

2. Handling File Uploads in Controllers

If the error occurs specifically during a file upload using req.file(), you should also pass the maxBytes option directly in your controller action:

// api/controllers/FileController.js
upload: function (req, res) {
  req.file('avatar').upload({
    maxBytes: 50000000 // Set limit to 50MB
  }, function (err, uploadedFiles) {
    if (err) return res.serverError(err);
    return res.json({
      message: uploadedFiles.length + ' file(s) uploaded successfully!'
    });
  });
}

Important Considerations:

  • Reverse Proxies: If you are deploying your Sails.js app behind Nginx or Apache, remember that they have their own limits (as mentioned previously). Even if Sails is configured for 50MB, Nginx will block the request at 1MB by default.
  • Memory Usage: Increasing these limits allows larger data chunks into your server's RAM. If you're running on a small VPS, keep an eye on memory consumption when processing large files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment