request entity too large error when using oas3-tools package,openapi 3.0 in node.js

346 Views Asked by At

index.js file

'use strict';

    var path = require('path');
    var http = require('http');
    var cors = require('cors');
    var oas3Tools = require('oas3-tools');

    require("dotenv").config({ path: ".env" });
    console.log(`### Running on ~~~ ${process.env.Instance} ~~~ ENV ###`);

    var serverPort = 8080;

    function validate(request, scopes, schema) {
        // security stuff here
        return true;
    }

    // swaggerRouter configuration
    var options = {
        routing: {
            controllers: path.join(__dirname, './controllers')
        },
        logging: {
            format: 'combined',
            errorLimit: 400
        },
    }; 


    var expressAppConfig = oas3Tools.expressAppConfig(
        path.join(__dirname, "./api/openapi.yaml"),
        options
      );
      expressAppConfig.addValidator();
      var app = expressAppConfig.getApp();

      app.use(cors());
      var json2xls = require('json2xls');
      app.use(json2xls.middleware);

    // Initialize the Swagger middleware
    http.createServer(app).listen(serverPort, function () {

    });


    // Connect to database 
    var mongo = require("./utils/db");

----------------------------------------EOF----------------------------------------------

PFB the version of the tools which we've used. "express": "^4.17.1", "oas3-tools": "2.1.3"

I've tried body-parser and app.use(express.json({limit:'25mb'})). But nothing resolved the request entity too large error. It would be great if anyone suggest me any different solution.

1

There are 1 best solutions below

0
On

This works for me:

app._router.stack = app._router.stack.filter((i) => i.name !== 'jsonParser');
app.use(bodyParser.json({ limit: 5 * 1024 * 1024 }));

You can see all the middleware in app._router.stack which is populated by expressAppConfig function (express.app.config.js).

I found out when you use bodyParser.json(), it will add another middleware layer to that stack - but from my observation, express will use the first one. That's why I removed all jsonParser before setting the limit.