1

Currently I'm increasing body size for all requests like this:

import Koa from 'koa';
import bodyParser from 'koa-bodyparser';
...
const app = new Koa();
...
app.use(
  bodyParser({
    jsonLimit: '150mb',
  })
);
...

I'm struggling to find a way to increase body size just for single Koa route not for all routes. Does anyone has a solution?

Matej Ukmar
  • 2,157
  • 22
  • 27
  • following link will help you: https://stackoverflow.com/questions/18950703/how-do-i-set-the-bodyparser-upload-limit-to-specific-routes-rather-than-globally – Abu Sufian Dec 03 '20 at 12:08
  • @AbuSufian This one is for express, and it does not work for Koa. At least I couldn't figure it out. – Matej Ukmar Dec 03 '20 at 13:30

1 Answers1

0

I've ended up doing it in this way:

  1. Prevent body parser for specific route for which you need bigger body size:
app.use(async (ctx, next) => {
  if (ctx.path === '/my/big/body/route') ctx.disableBodyParser = true;
  await next();
});
app.use(bodyparser());
  1. Parse body with co-body on actual endpoint:
import { json } from 'co-body';

export default async function uploadImage(ctx, next) {
  try {
    const body = await json(ctx, { limit: '150mb' });
...
Matej Ukmar
  • 2,157
  • 22
  • 27