Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the implementation principle of koa middleware

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to implement koa middleware for you. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The order in which koa is executed is as follows:

Const middleware = async function (ctx, next) {

Console.log (1)

Await next ()

Console.log (6)

}

Const middleware2 = async function (ctx, next) {

Console.log (2)

Await next ()

Console.log (5)

}

Const middleware3 = async function (ctx, next) {

Console.log (3)

Await next ()

Console.log (4)

}

Will print 1, 2, 3, 4, 5, 6 in turn.

The question is, what is the implementation principle of koa middleware, that is, the onion model?

I. problem analysis

Async await is the syntax sugar of promise, and await is followed by a promise, so the above code can be written as follows:

Const middleware = function (ctx, next) {

Console.log (1)

Next () .then (() = > {

Console.log (6)

})

}

Const middleware2 = function (ctx, next) {

Console.log (2)

Next () .then (() = > {

Console.log (5)

})

}

Const middleware3 = function (ctx, next) {

Console.log (3)

Next () .then (() = > {

Console.log (4)

})

}

It is easier to understand it this way, so the core of process control is the implementation of next.

Next requires that the next middleware in the queue be called and resolve when the last one is reached. So the last promise is resolve first, all the way to the first one, so that's the order of the onion model.

II. Realization

The implementation of koa-compose is as follows:

Function compose (middleware) {

Return function (context, next) {

Let index =-1

Return dispatch (0)

Function dispatch (I) {

Index = I

Let fn = middleware [I]

If (I = middleware.length) fn = next

If (! fn) return Promise.resolve ()

Try {

Return Promise.resolve (fn (context, dispatch.bind (null, I + 1)

} catch (err) {

Return Promise.reject (err)

}

}

}

}

We have removed some of the non-core logic for parameter checking, and the implementation code is just the one above. Each incoming next calls the next middleware, which is a recursive process, and the end condition is that the next of the last middleware is passed in by the user.

There are some bright spots here:

This is a form of tail recursion, which is characterized by the fact that the final returned value is a recursive function call, so that execution will be destroyed in the call stack and will not occupy the call stack.

What is returned is a Promise.resolve wrapped call, not a synchronous call, so this is an asynchronous recursion. The advantage of asynchronous recursion over synchronous recursion is that it can be interrupted. If there are some higher priority micro-tasks in the middle, you can perform other micro-tasks first.

Compose is a functional composition, the n middleware composite into one, the parameters are still context and next, this composite is still a middleware, you can continue to composite.

This is the end of the article on "how to implement koa middleware". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report