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

Example Analysis of flow Control of middleware in koa

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

Share

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

This article mainly shows you the "koa middleware process control example analysis", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "koa middleware process control example analysis" this article.

1. Koa middleware execution process

There is a very classic example of how to implement koa middleware on the official website, which you can take a look at if you are interested, but here, I want to make it a little easier:

Var koa = require ('koa'); var app = koa (); app.use (function* (next) {console.log (' begin middleware 1'); yield next;console.log ('end middleware 1');}); app.use (function* (next) {console.log (' begin middleware 2'); yield next;console.log ('end middleware 2');}); app.use (function* () {console.log (' middleware 3');}); app.listen (3000)

Run this example, then use the curl tool, and run:

Curl http://localhost:3000

As you can see, after running, the output is:

Begin middleware 1begin middleware 2middleware 3end middleware 2end middleware 1

This example is a vivid representation of koa's middleware execution mechanism, which can be described by the onion model shown below:

Through this execution process, developers can easily develop some middleware and integrate it into the actual business process. So, how is such a process realized and controlled?

2. Generator and compose in koa

To put it simply, the execution process of the onion model is implemented through generator in es6. Students who are not familiar with generator can take a look at its features, one of which is that the generator function can jump out of somewhere like a breakpoint, and then come back to continue execution. The following example illustrates this feature:

Var gen=function* () {console.log ('beginnings'); / / yield statement, which pops out here and gives control to the anotherfunc function. The next time yield anotherfunc;// comes back, execute console.log ('endgame');} var anotherfunc () {console.log ('this is another functionalization');} var g=gen (); var another=g.next (); / / 'beginbread value' is an object, where the value member is the returned anotherfunc function another.value (); / / 'this is another functionfunctionfunctionaccount.next (); / /' endgame'

From this simple example, you can see the most basic prototype of the onion model, that is, the statements before and after the yield are executed first and last, and the code in the middle of the yield is executed at the center.

Now imagine what would happen if the function followed by yield was itself another generator? In fact, it is an extension from the above example:

Var gen1=function* () {console.log ('beginnings'); yield g2 console.log ('endurance');} var gen2=function* () {console.log ('begin 2'); yield anotherfunc;console.log (' end 2');} var anotherfunc () {console.log ('this is another functionalities');} var g=gen (); var g2=gen2 (); var another1=g.next (); / / 'beginnings accountabilitanother2.value () / / 'this is another functionalizativeanother1.value.next (); / /' end 2 accountabilityg.Next (); / / 'endgame'

As you can see, basically using the above example, plus a nesting, the principle is the same.

In koa, each middleware generator has a next parameter. In our example, G2 can be thought of as the next parameter of the g function. In fact, koa does this. After all the middleware is mounted with app.use (), koa has a koa-compose module for concatenating all the generator middleware, basically assigning the latter generator to the next parameter of the previous generator. The source code of koa-compose is very simple and short. Here is one I have implemented by myself:

Function compose (middlewares) {return function (next) {var I = middlewares.length;var next = function* () {} (); while (iMury -) {next = lewares [I] .call (this, next);} return next;}}

Use our own compose to modify the above example, yes, it is closer to the form of koa:

Function compose (middlewares) {return function (next) {var I = middlewares.length;var next = function* () {} (); while (iMuk -) {next = roomlewares [I] .call (this, next);} return next;}} var gen1=function* (next) {console.log ('beginnings'); yield next;console.log ('endurance');} var gen2=function* (next) {console.log ('begin 2'); yield next;console.log (' end 2') } var gen3=function* (next) {console.log ('this is another functionalities');} var bundle=compose ([gen1,gen2,gen3]); var g=bundle (); var another1=g.next (); / / 'beginnings investors another2=another1.value.next (); / /' begin 2 investors investors another2.value.Next (); / / 'this is another functionalities transmissible another1.value.next (); / /' end 2 accountability.Next (); / / 'endgame'

How's it going? Does it feel a little bit like koa middleware? But at present, we are still implementing our onion model step by step manually. Can we write a function to execute our model automatically?

3. Make the Onion Model run automatically: the compilation of a run function

In the above example, we can see a rule in the final code. Basically, the outer generator calls the next method to give control to the inner layer, and the inner layer continues to call next to give the method to the inner layer. The whole process can be written in a function-nested way. Don't say much, just go to the code:

Function run (gen) {var gbot if (typeof gen.next = 'function') {g = gen;} else {g = gen ();} function next () {var tmp = g.next (); / / if tmp.done is true, prove that generator execution ends and returns. If (tmp.done) {return;} else if (typeof g.next = 'function') {run (tmp.value); next ();}} next ();} function compose (middlewares) {return function (next) {var I = middlewares.length;var next = function* () {} (); while (iMuk -) {next = lewares [I]. Call (this, next);} return next;}} var gen1 = function* (next) {console.log (' beginwax'); Console.log ('endgame');} var gen2 = function* (next) {console.log ('begin 2'); yield next;console.log (' end 2');} var gen3 = function* (next) {console.log ('this is another functionalities');} var bundle = compose ([gen1, gen2, gen3]); run (bundle)

The run function accepts a generator, and its internal execution is actually a simplification of our previous example, using a recursive method. Run this example and you can see that the result is the same as our previous example.

So far, we've basically made it clear how the middleware onion model in koa is executed automatically. In fact, part of the function of the co function used in koa is to implement the function of the run function we wrote here.

The above is all the contents of the article "sample Analysis of koa Intermediate flow Control". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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