Error 500 on the auth route when using bodyparser middleware

Feature(s) impacted

Authentication

Observed behavior

Say we start the server this way:

  ...
  const agent = await buildAgent();
  const app = express();
  await agent.mountOnExpress(app).start();
  app.listen(env.PORT);
  ...

Now I want to add a middleware to control the JSON payload limit like:

  const agent = await buildAgent();
  const app = express();
  app.use(express.json({ limit: '5mb' }));
  await agent.mountOnExpress(app).start();
  app.listen(env.PORT);

Now the server will return a code 500 on the route /forest/authentication.

Expected behavior

The route /forest/authentication returns a code 200.

Failure Logs

  InternalServerError: stream is not readable
      at readStream (/project/node_modules/raw-body/index.js:178:17)
      at executor (/project/node_modules/raw-body/index.js:113:5)
      at new Promise (<anonymous>)
      at getRawBody (/project/node_modules/raw-body/index.js:112:10)
      at module.exports [as json] (/project/node_modules/co-body/lib/json.js:39:21)
      at parseBody (/project/node_modules/koa-bodyparser/index.js:100:26)
      at bodyParser (/project/node_modules/koa-bodyparser/index.js:85:25)
      at dispatch (/project/node_modules/koa-compose/index.js:42:32)
      at /project/node_modules/@koa/router/lib/router.js:372:16
      at dispatch (/project/node_modules/koa-compose/index.js:42:32)

Context

  • Environment name: dev
  • Agent type & version:
"@forestadmin/agent": "^1.4.5", 
"express": "~4.17.1",

Thanks for your help :slight_smile:

Hello @Aldric_Libonis,

The first issue would lie in the fact that you have to use our middleware before any other like shown on our documentation. Our middleware is also setting a limit to 50mb, this may conflict with what you might try to achieve and sadly for the moment this is not configurable.

Best regards,

Dogan

Hello @dogan.ay, once again thanks for you swift answer.

Best regards,

1 Like