Home >Web Front-end >JS Tutorial >An in-depth analysis of the 'onion model' in Nodejs
This article will take you through the "onion model" in Nodejs. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Currently the more popular Node.js frameworks include Express, KOA and Egg.js, followed by another emerging TypeScript-related framework - Nest.js. No matter which Node.js framework it is, it is implemented based on middleware, and the execution method of middleware (can be understood as a class or function module) needs to be based on the onion model
Recommended learning: "nodejs tutorial》
We all know that onions are wrapped one layer at a time, layer by layer, but now we are not looking at its three-dimensional structure, but It is necessary to cut the onion. From the cutting plane, as shown in the picture:
You can see that in order to pass through the center of the onion, you must first The onion skin is penetrated layer by layer into the center point, and then the skin is penetrated outward layer by layer from the center point. There is a characteristic here: how many layers of skin are penetrated when entering, how many layers of skin must be penetrated when exiting. . Penetrating the epidermis first and then exiting it conforms to the principle of what we call a stack list, first in, last out.
The more popular Node.js frameworks currently include Express, KOA and Egg.js. No matter which Node.js framework it is, it is based on middleware To achieve, the execution method of middleware (can be understood as a class or function module) needs to be based on the onion model
We can think of the skin of the onion as middleware:
The process from the outside to the inside is the keyword next();
, while the process from the inside to the outside means that after each middleware is executed, it enters the original upper layer of middleware, all the way to the outermost layer.
Taking express as an example, the following is a basic execution process of middleware:
、
Koa is the next generation node framework developed by the same team based on Express. The main difference between the two is:
Introduction to the differences between Express and KOA regarding the execution of the onion model:
We retain the original three of the above example code middleware, and insert a new asynchronous middleware between 2 and 3. The code is as follows:
(1) express
/** * 异步中间件 */ app.use(async (req, res, next) => { console.log('async'); await next(); await new Promise( (resolve) => setTimeout( () => { console.log(`wait 1000 ms end`); resolve() }, 1000 ) ); console.log('async end'); });
and then Other middlewares are modified to await next() method, as shown in the following middleware 1 method:
/** * 中间件 1 */ app.use(async (req, res, next) => { console.log('first'); await next(); console.log('first end'); });
Rerun, the final output result is:
You can see Out, it is normal to call from outside to inside, and calls are made layer by layer. When going from inside to outside, some changes occur. The main reason is that asynchronous middleware does not output execution results in order.
(2) KoaKeep the above code sequence and only change the corresponding express syntax to koa syntax. The middleware 1 and asynchronous middleware code parts are as follows:
const Koa = require('koa'); const app = new Koa(); /** * 中间件 1 */ app.use(async (ctx, next) => { console.log('first'); await next(); console.log('first end'); }); /** * 异步中间件 */ app.use(async (ctx, next) => { console.log('async'); await next(); await new Promise( (resolve) => setTimeout( () => { console.log(`wait 1000 ms end`); resolve() }, 1000 ) ); console.log('async end'); });
Rerun, the final output result is:
You will find that KOA strictly follows the execution of the onion model, from top to bottom, That is, from the inside of the onion to the outside, output first, second, async, and third; then output third end, async end, second end, and first end from the inside to the outside.
For more programming-related knowledge, please visit: Programming Teaching! !
The above is the detailed content of An in-depth analysis of the 'onion model' in Nodejs. For more information, please follow other related articles on the PHP Chinese website!