var app = require('koa')();
var router = require('koa-router');
app.use(router(app));
Throws this error:
AssertionError: app.use() requires a generator function
A lot of sample code says to setup koa-router this way. It supposedly adds methods to the koa app.
The koa-router package changed a few months back and removed the functionality to extend the app object, as you've coded above... It used to work that way, but it was a breaking change:
http://github.com/alexmingoia/koa-router/issues/120.
Here is an example of how you setup routes now:
var app = require('koa')();
var router = require('koa-router');
// below line doesn't work anymore because of a breaking change
// app.use(router(app));
var api = router();
api.get('/', function *(){
this.body = 'response here';
});
app
.use(api.routes())
.use(api.allowedMethods());
app.listen(3000);
First, change your:
var router = require('koa-router');
to
var router = require('koa-router')();
After that, insert some router rule, for example:
router.get('/', function *(next) {
this.status = 200;
this.body = {"Welcome":"Hello"};
});
And at the end of all this write: app.use(router.routes());
- this line is a key factor here... And you're all set.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With