我正在尝试使用 Koa.js,并检查了以下用于路由请求的模块: 1. koa-route 2. koa-mount
当我在 google 中查看他们的 github 页面/教程时,这些示例看起来几乎相似,但略有不同。
对于 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)对于 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 需要一个生成器来处理请求。
我很困惑何时使用什么以及何时使用两者(在某些教程中看到过)?
| 归档时间: |
|
| 查看次数: |
2102 次 |
| 最近记录: |