How to use this middleware with koa-bodyparser
See original GitHub issueHello 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:
- Created 7 years ago
- Reactions:7
- Comments:16 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 injoi-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.
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.