question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use this middleware with koa-bodyparser

See original GitHub issue

Hello and thank you all for the work done on this project.

I’m trying to integrate this middleware into a project that already uses koa-bodyparser (required middleware to koa-passport).

Unfortunately when trying to reach a route when this middleware is declared the road never answers. I guess there is a conflict with co-body somewhere.

Do we have a workaround for using this middleware with koa-bodyparser or am i going wrong ?

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

const app = koa();
const helloRouter = router();

app.use(bodyParser()); // <-- comment and the call will work

helloRouter.route({
  method: 'post',
  path: '/hello',
  validate: {
    body: {
      name: Joi.string().required()
    },
    type: 'json'
  },
  handler: function* hello() {
    const { name } = this.request.body;
    this.body = `Hello ${name}`;
  }
});

app.use(helloRouter.middleware());

app.listen(3000);

The call : curl -X POST -H "Content-Type: application/json" -d '{ "name": "John" }' "http://localhost:3000/hello"

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:7
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
hilkeheremanscommented, Feb 2, 2018

I’m using koa-body and had the same issue. Following @yelworc’s suggestion, I forked the repo and skipped the body parsing step should it already be parsed.

Seems to work fine with validation still intact, although I should note that limits on body size (maxBody option) should no longer be configured in joi-router at that point, but rather in whatever body parser you use.

See also https://github.com/hilkeheremans/joi-router or npm @thinman/koa-joi-router

Holding off on a PR until one of the contributes chimes in with an opinion.

2reactions
pkecommented, Oct 10, 2018

Maybe this lib should not even depend on a body parser at all but rather integrate into whatever is available upstream in middleware. So it could just take body as being parsed already and validate it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

koa body parser middleware - GitHub
A full-featured koa body parser middleware. Supports multipart , urlencoded , and json request bodies. Provides the same functionality as Express's ...
Read more >
How to use the koa-bodyparser function in koa ... - Snyk
To help you get started, we've selected a few koa-bodyparser examples, ... app.use(cookie()); // load any user defined middleware const middleware ...
Read more >
koa-bodyparser JavaScript and Node.js code examples
Best JavaScript code snippets using koa-bodyparser(Showing top 15 results out of 315) ... describe('together the xss and sqlInjection middleware', ...
Read more >
I can't access my request.body object in Koa JS (I am using ...
To access your body object, try the following: const body = ctx.request.body;. Secondly: because in one of the comments there was a note,...
Read more >
TypeScript with Koa: Part — 2. Type Definitions ... - Medium
If we use the body parser middleware, it will parse the incoming payload according to it's content type and store the content in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found