express VS koa

框架的功能上:

Koa更像一个中间件框架,其提供的是一个加载,而几乎所有的功能都需要由第三方中间件完成
Express则更贴近Web Framework,它自带Router、路由规则等。

const express = require("express");
const app     = express();
const router  = express.Router();

app.use(中间件);

route.use((req, res, next)=>{
    ...
    next(); //需要手动
});
handler的处理方法:

Koa是利用生成器函数(Generator Function)来作为响应器,Express则是普通的回调函数。
Express是再同一个县城上完成当前进程的所有HTTP请求,而Koa利用co作为底层运行框架(Koa 2.0不再使用co),利用Generator的特性,实现“协程响应”

  • 延伸:Koa 2.0与Koa 1.x版本的最大区别就是使用了ES7中Async/Await的特性,代替co的Generator Function,好处是摆脱了co的“暧昧”实现方法
缺点:
  • Express最大的问题就是回调地狱(callback hell)问题。但其实可以通过eventproxy解决这一问题。
  • Express的第二个问题就是异常不可捕获。
  • 而Koa最大的优势就是解决了回调地狱问题。

————Koa尚未使用,待完善————

  • 延伸:使用EventProxy模块控制并发
const eventproxy = require("eventproxy");
const ep = new eventproxy();
$.get("template", function (template) {
  // something
  ep.emit("template", template);
});
$.get("data", function (data) {
  // something
  ep.emit("data", data);
});
ep.all("template","data",(template,data)=>{

});

猜你喜欢

转载自blog.csdn.net/csm0912/article/details/80314526