Problems with koa-better-router?
See original GitHub issueHi,
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
import koa from 'koa';
import body from 'koa-better-body';
import Router from 'koa-rest-router';
import convert from 'koa-convert';
const app = new koa();
apiRouter.resource('users', {
index: [
(ctx, next) => {
ctx.body = "list users!\n";
return next();
},
function *(next) {
this.body = `${this.body} and blah blah..`;
yield next;
}
],
recent: (ctx, next) => { ctx.body = "Newly created user!\n"; return next(); },
create: (ctx, next) => { ctx.body = "Create users!\n"; return next(); },
edit: (ctx, next) => { ctx.body = "Edit user view!\n"; return next(); },
update: [body,
(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;
}
],
remove: (ctx, next) => { ctx.body = "remove users!\n"; return next(); }
}, {map: {'new': 'recent'}, methods: {'put': 'post'}});
const basic = new Router(); //- For routes not beginning with /api/v1
basic.extend(apiRouter);
app.use(convert(body())); //- did this out of desperation
app.use(apiRouter.middleware());
app.use(basic.middleware());
This code is from chunks of your koa-better-router
examples and problem comes when i use update
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 cause update
controller wasn’t the one called (no controller was).
update: [body,
(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;
}
],
If i remove the “body” from the update
and call this.request.fields
i get error.
Update code when i remove body
update: [
(ctx, next) => {
debugger;
ctx.body = "create users!\n";
return next();
},
function *(next) {
this.body = `body from req ${this.request.fields}`;
console.log(this.request);
yield next;
}
],
Error Msg when i remove body
TypeError: Cannot convert object to primitive value
at Object._callee2$ (/home/users/Projects/koa/test-project1/dist/index.js:70:54)
at tryCatch (/home/users/Projects/koa/test-project1/node_modules/regenerator-runtime/runtime.js:64:40)
at Generator.invoke [as _invoke] (/home/users/Projects/koa/test-project1/node_modules/regenerator-runtime/runtime.js:355:22)
at Generator.prototype.(anonymous function) [as next] (/home/users/Projects/koa/test-project1/node_modules/regenerator-runtime/runtime.js:116:21)
at onFulfilled (/home/users/Projects/koa/test-project1/node_modules/co/index.js:65:19)
at /home/users/Projects/koa/test-project1/node_modules/co/index.js:54:5
at Object.co (/home/users/Projects/koa/test-project1/node_modules/co/index.js:50:10)
at converted (/home/users/Projects/koa/test-project1/node_modules/koa-convert/index.js:17:15)
at dispatch (/home/users/Projects/koa/test-project1/node_modules/koa-compose/index.js:44:32)
at next (/home/users/Projects/koa/test-project1/node_modules/koa-compose/index.js:45:18)
Please help.
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Issues · tunnckoCore/koa-better-router - GitHub
heart: Stable and lovely router for `koa`, using `path-match`. Foundation for building powerful, flexible and RESTful APIs easily. - Issues ...
Read more >koa-better-router vs koa-route vs koa-router - npm trends
This site was created by John Potter and is maintained by uidotdev. If you find any bugs or have a feature request, please...
Read more >koa-easy-logger - npm Package Health Analysis - Snyk
The npm package koa-easy-logger was scanned for known vulnerabilities and missing license, and no issues were found. Thus the package was deemed ...
Read more >Building a Node.js REST Microservice: Part One - Coveros
koa-better-router – This module enhances the routing capabilities of Koa, and provides a clean and simple way to organize the REST routes.
Read more >eslint-config-standard-tunnckocore | Yarn - Package Manager
You might also be interested in koa-better-router. ... any how-to kind of questions, please read Code of Conduct and join the chat room...
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
Haha, thanks 🗡️ Hope they help.
Hi there, sorry for confusing. I can’t believe that people face issues and misunderstand some things, with this great readmes. Anyway 😆
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 bebody()
. And fourth, you don’t need to add both middlewares to the Koa app - I’ve seen you add bothbasic.middleware()
andapiRouter.middleware()
.And finally, I believe I put somewhere working example.
edit: Yea, see the recipes of
koa-better-router
- recipes/adding-a-route