Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

koa

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)?

like image 296
Kop4lyf Avatar asked Apr 27 '15 10:04

Kop4lyf


1 Answers

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.

like image 173
Molda Avatar answered Oct 25 '22 02:10

Molda