Koa : koa-route 和 koa-mount 有什么区别。我应该什么时候使用?

Kop*_*lyf 5 node.js koa

我正在尝试使用 Koa.js,并检查了以下用于路由请求的模块: 1. koa-route 2. koa-mount

当我在 google 中查看他们的 github 页面/教程时,这些示例看起来几乎相似,但略有不同。

  1. 对于 koa 路线:

    var route = require('koa-route');
    app.use(route.get('/', index));
    
    //functions to handle the request
    function* index(){
        this.body = "this should be home page!";
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 对于 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';
    });
    
    Run Code Online (Sandbox Code Playgroud)

在我看来唯一的区别是 mount 需要一个中间件来处理请求,而 route 需要一个生成器来处理请求。

我很困惑何时使用什么以及何时使用两者(在某些教程中看到过)?

Mol*_*lda 6

Koa-mount 的目的是将一个应用程序挂载到另一个应用程序中。例如,您可以创建独立的博客应用程序并将其安装到另一个应用程序。您也可以挂载其他人创建的应用程序。