The KOA mentioned in this paper are all KOA2
When it comes to Nodejs, we all know about Express and KOA.
Express: Big
KOA: Small
The comparison is the function, community, middleware, related resources, etc.
Here I have a special talk about middleware, many people may say that express plugin rich AH. In fact, in addition to middleware, the rest of what and express KOA itself does not have much to do with, not all based on Nodejs http (https) encapsulation.
Express middleware is very rich, but to wake up, express middleware can be run under the KOA. Here we have to mention Koa-connect.
So, let's take a quick look at how this koa-connect is implemented.
Its source code altogether only 38 lines, half many comments, 3 methods.
- Koaconnect: The external method, the analysis of the parameters of Express middleware, call Nocallbackhandler and Withcallbackhandler respectively
- Nocallbackhandler: Middleware for processing express without callbacks
- Withcallbackhandler: Middleware for processing express with callbacks
The callback here is there is no next method, next method is to enter the next middleware
/** * If The middleware function does declare receiving the ' next ' callback * assume that it ' s synchronous and invoke ' NEX T ' ourselves */function Nocallbackhandler (CTX, Connectmiddleware, next) {Connectmiddleware (ctx.req, ctx.res) return NEX T ()}/** * The middleware function does include the ' next ' callback so only resolve * the Promise when it ' s called. If it ' s never called, the middleware stack * completion would stall */function Withcallbackhandler (CTX, Connectmiddleware, Next) {return new Promise ((resolve, reject) = {Connectmiddleware (ctx.req, ctx.res, err = = {if (err) Rej ECT (ERR) Else resolve (next ())})}/** * Returns a KOA middleware function that varies it async logic based on If the * given middleware function declares at least 3 parameters, i.e. includes * the ' next ' callback function */function Koaconnect (connectmiddleware) {Const Handler = Connectmiddleware.length < 3? nocallbackhandler:withcallback Handler return function KoaconNect (CTX, next) {return handler (CTX, Connectmiddleware, next)}}module.exports = Koaconnect
Koaconnect method
function koaConnect(connectMiddleware) { const handler = connectMiddleware.length < 3 ? noCallbackHandler : withCallbackHandler return function koaConnect(ctx, next) { return handler(ctx, connectMiddleware, next) }}
Kaoconnect returns a KOA version of the middleware.
Connectmiddleware.length is the length of the Express middleware parameter, and if you use arguments for parameter parsing or using rest parameters, the length itself is accurate.
Callback and no callback methods are called by the length of the Express middleware parameters.
Nocallbackhandler method
function noCallbackHandler(ctx, connectMiddleware, next) { connectMiddleware(ctx.req, ctx.res) return next()}
Call the Express method directly, Ctx.req and ctx.res as parameters passed in.
The request object for the Ctx.req:Node.
The Reponse object of the Ctx.res:Node.
This shows that the ctx.req and res of the intermediate req and res and KOA middleware in Express are a thing.
Because the Express middleware does not call next, it is called to call the next KOA middleware directly
Withcallbackhandler method
function withCallbackHandler(ctx, connectMiddleware, next) { return new Promise((resolve, reject) => { connectMiddleware(ctx.req, ctx.res, err => { if (err) reject(err) else resolve(next()) }) })}
Returns a promise because there is a call to await next () in KOA, which satisfies the requirement well.
In the middle of express, if there is a third parameter next, the call represents the next middleware.
Express middleware next call can pass in error,
You can refer to express error handling
app.get("/", function (req, res, next) { fs.readFile("/file-does-not-exist", function (err, data) { if (err) { next(err); // Pass errors to Express. } else { res.send(data); } });});
Back to our Withcallbackhandler method.
The next method of express middleware here is
err => { if (err) reject(err) else resolve(next()) }
When there's a mistake, reject
When there is no error, call KOA Middleware Next and continue with the execution below.
Koa-connect analysis is complete, the core is two points
- Differentiate processing by the parameter length of the Express middleware
- Next approach to transforming express middleware
Koa-connect Source Code Analysis