Search code examples
node.jskoa

Koa : what is the difference between koa-route and koa-mount. When should I use each?


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.

  1. 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!";
    }
    
  2. 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)?


Solution

  • 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.