koajs / bodyparser

Koa body parsing middleware
MIT License
1.31k stars 116 forks source link

Bodyparser causing 404 error if `await` is not used with `next` in any previous middleware #55

Closed wlingke closed 7 years ago

wlingke commented 7 years ago

For this simple koa2 server:

app.use(async (ctx, next) => {
  next();
});

app.use(bodyParser({ enableTypes: ['json'] }));
app.use(async (ctx) => (ctx.status = 200));

Client calls:

fetch('/', {method: 'post', headers: {'Content-Type': 'application/json'}})

The return is status 404 - Not Found.

If I change the first middleware by adding await:

app.use(async (ctx, next) => {
  await next();
});

then it works as expected.

waliurjs commented 7 years ago

same here. Only works with Content-Type: text/plain

dead-horse commented 7 years ago

you must add yield before next() here, because bodyparser is asynchronous.