美文网首页
Express 和 Koa 的比较

Express 和 Koa 的比较

作者: 萧哈哈 | 来源:发表于2018-08-26 19:46 被阅读220次

节选自 loopback4-improves-inbound-http-processing


Traditionally in Express (and Connect), functions implementing request handling logic have the following signature:

function (req, res, next);

While it’s amazing how powerful this simple signature is, it comes with major flaws too:

  1. Because the list of arguments is given, it’s difficult to pass additional data between different handlers. The convention is to attach custom properties to the Request object, e.g. body-parsing middleware sets req.body property. This approach is difficult to describe in TypeScript and the ever-changing shape of the Request object has negative impact on (micro)performance.

  2. Flow control is difficult to map to Promises and async/await. Execution of a middleware handler has three possible outcomes:

    i. The request was handled and the remaining items in the middleware chain are skipped. Implementation wise, middleware did not call next().

    ii. The middleware modified the request/response objects, or a route did not match the requested path, and the next handler in the middleware chain should be executed. Implementation wise, middleware called next() with no arguments.

    iii. There was an error and request handling should be aborted. Implementation wise, middleware called next() with a single argument - the error.

It is difficult to run a piece of code after the request was handled or transform the response before it’s being sent. For example, to print a log line containing information about the request including timing information, one has to register the logging middleware early in the middleware chain so that it can register event observers and/or monkey-patch request/response objects before any actual work is done - this is counter-intuitive since the log line is printed at the end of request handling.

Koa addresses these flaws by introducing an extensible Context object that contains not only the request and response, but also additional properties and helper methods.


相关文章

  • Express 和 Koa 的比较

    节选自 loopback4-improves-inbound-http-processing Traditiona...

  • koa.js的使用(koa2)

    koa与Express简单比较Express connect 中间件 封装了 路由、视图,koa co中间件 不包...

  • koa-router的使用

    Koa中的路由和Express不同,Express是把路由集成在Express中,Koa则需要通过kao-rout...

  • node学习三(koa使用入门)

    express和koa文档: express: http://www.expressjs.com.cnkoa: h...

  • koa中间件-express中间件

    node开启http服务 koa开启http服务 express中间件 koa与express

  • koa入门

    koa介绍 Koa是由Express背后的团队创建的新流行的Web应用框架。它旨在成为Express的现代和极简主...

  • nodejs框架对比:koa和express

    目前比较流行的nodejs框架有express、koa、egg.js,还有就是和ts相关的框架nest.js。 无...

  • Express Koa2 Egg 对比

    目前比较流行的nodejs框架有express、koa、egg.js,还有就是和ts相关的框架nest.js。 无...

  • express和koa的区别

    express和koa从整体上来看,koa是比express更加轻量,他没有内置的各种中间件的支持,更集中于请求处...

  • express 和 koa

    express req.cookies cookie-parser 处理cookie挂载 req.query, r...

网友评论

      本文标题:Express 和 Koa 的比较

      本文链接:https://www.haomeiwen.com/subject/hktaiftx.html