I am trying to use Koa.js, and checked out the following modules for routing requests: 1. koa-route 2. koa-mount
When I check their github page/tutorials in google, the examples look almost similar with minor differences.
For koa-route:
var route = require('koa-route');
app.use(route.get('/', index));
//functions to handle the request
function* index(){
this.body = "this should be home page!";
}
For koa-mount:
//syntax to add the route
var mount = require('koa-mount');
var a = koa();
app.use(mount('/hello', a));
//functions to handle the request
a.use(function *(next){
yield next;
this.body = 'Hello';
});
The only difference seems to me is mount needs a middleware to serve the request, while route needs a generator to serve the requests.
I am confused when to use what and when to use both(saw that in some tutorials)?
Koa-mount's purpose is to mount one app into another. For example you can create standalone blog app and mount it to another app. You can mount apps others have created too.
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