Closed maotora closed 7 years ago
Hi there, sorry for confusing. I can't believe that people face issues and misunderstand some things, with this great readmes. Anyway :laughing:
First, you missed to put what apiRouter
(I know what it is, just note). Second, you don't need to create two routers - basic and apiRouter. Third, body
is not called, it should be body()
. And fourth, you don't need to add both middlewares to the Koa app - I've seen you add both basic.middleware()
and apiRouter.middleware()
.
And finally, I believe I put somewhere working example.
edit: Yea, see the recipes of koa-better-router
- recipes/adding-a-route
@tunnckoCore ooh man 🗡 You are awesome!
Thank you!
Haha, thanks :dagger: Hope they help.
@tunnckoCore i'm passing over some examples, and regarding your comment "Third, body
is not called, it should be body()
".
What did you mean? As in you meant in the app.use(body())
or in the update: [body, ...]
controller?
in the controller should be body()
too. Because it returns a middleware function only when is called.
@tunnckoCore solved it today.. hehe
import body from 'koa-better-body';
body
from update
controller in koa-better-router
.api.resource('users', {
update: //- From here.
(ctx, next) => {
ctx.body = "create users!\n";
console.log('your data ', ctx.request.fields);
return next();
}
});
app.use(convert(body({
multipart: true,
})));
and this solved everything.
All other tries (including body
in update
controller, not calling body
in middleware etc..) had some failures in one way or another.
Thank you for this middleware man :)
It's not cool in that way. Because it will be called in requests that not need body parsing.
Also you dont need koa-convert
if you including it in controller. We use that package internally.
One note on
from update controller
it's not "update controller", it's "update method" from users controller.
What's so hard in that
import bodyParser from 'koa-better-body'
api.resource('users', {
update: [
bodyParser({ some: 'options' }),
(ctx, next) => {
ctx.body = "create users!\n"
return next()
},
function * (next) {
this.body = `And incoming data is ...`
this.body = `body from req ${this.body} ${this.request.fields}`
yield next
}
]
})
Hi,
Awesome middleware you made here!
I'm starting with koa and your middlewares seem pretty great so i'm using
koa-better-router
&koa-better-body
together.However things are not working as expected. I'm
This code is from chunks of your
koa-better-router
examples and problem comes when i useupdate
which is using body.If i include "body" as the beginning arg in
update
and post something to/users/1
i get Not Found and nothing gets run causeupdate
controller wasn't the one called (no controller was).If i remove the "body" from the
update
and callthis.request.fields
i get error. Update code when i removebody
Error Msg when i remove
body
Please help.