koajs / joi-router

Configurable, input and output validated routing for koa
MIT License
450 stars 96 forks source link

request hangs if joi-router used together with a body parser #63

Closed piotr-s-brainhub closed 6 years ago

piotr-s-brainhub commented 6 years ago

NodeJS: 8.6.0 npm: 5.2.0 OS: Max OS X 10.12.2 koa: 2.4.1 koa-joi-router: 5.0.0 koa-bodyparser: 4.2.0 koa-body: 2.5.0

For the following app:

const koa = require('koa');
const router = require('koa-joi-router');
const bodyParser = require('koa-bodyparser');

const public = router();

public.route({
  method: 'post',
  path: '/foo',
  validate: {
    type: 'json',
  },
  handler: async (ctx) => {
    ctx.status = 201;
  }
});

const app = new koa();
app.use(bodyParser());
app.use(public.middleware());
app.listen(2999);

the request hangs. The same if koa-body used instead if koa-bodyparser.

I need a koa body parser to be able to use request body in the middlewares.

piotr-s-brainhub commented 6 years ago

I fixed it in https://github.com/koajs/joi-router/pull/64

aheckmann commented 6 years ago

If you do not want joi-router to parse the payload, do not specify validate.type. See https://github.com/koajs/joi-router#handling-non-validated-input

chenzhutian commented 6 years ago

But if I do not specify the validate.type, the joi-router throw an error says "validate.type must be declared when using validate.body"

Sleepful commented 3 years ago

@aheckmann that does not work.