In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "the use, principle and implementation of Express middleware". In daily operation, I believe that many people have doubts about the use, principle and implementation of Express middleware. I have consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about the use, principle and implementation of Express middleware. Next, please follow the editor to study!
Now it is very common to use nodejs for server-side development in some enterprises. Of course, the most popular framework is express. Express simply encapsulates the native http modules supported by nodejs, making it easy for developers to use.
The most convenient of these is the middleware mechanism of express. Before introducing the principle and implementation of express middleware, let's take a look at how express middleware is used.
The middleware mechanism of express is like a funnel device. After a request arrives at the server, the request will be abstracted into a req object, which will enter the middleware at once, be processed separately in the middleware, and finally be distributed by the routing function.
As shown in the figure:
The code demonstration is shown in the figure:
After starting the express service, the middleware will be executed regardless of the root path "/" or "/ a", which means that the middleware will be executed one by one each time the service is requested.
So what is the purpose of this? To achieve a requirement, we need to calculate the pv of the entire site, that is, the entire site has been requested by the client many times, if we do not use middleware will do? Look at the code:
What if middleware is used? Take a look at the following code:
Take a closer look at the difference between the two code, the second is obviously much simpler, the calculation of the number of visits to the code into the middleware, no longer need to write in the various routes to write, to improve the reusability, logical expression is clearer, easy to maintain, the above is the use of express middleware, it should be noted here that the order of middleware calls is from top to bottom, each middleware call must be called next.
So what is the principle of the middleware mechanism and how is it implemented?
Take a look at the following code:
Var http = require ('http')
Function express () {
Var funcs = []; / / an array of functions to be executed
Var app = function (req, res) {
Var I = 0
Function next () {
Var task = funcs [iTunes +]; / / fetch the next function in the function array
If (! task) {/ / if the function does not exist, return
Return
}
Task (req, res, next); / / otherwise, execute the next function
}
Next ()
}
App.use = function (task) {
Funcs.push (task)
}
Return app; / / returns the instance
}
Var app = express ()
Function middlewareA (req, res, next) {
Console.log ('middleware 1')
Next ()
}
Function middlewareB (req, res, next) {
Console.log ('Middleware 2')
Next ()
}
Function middlewareC (req, res, next) {
Console.log ('middleware 3')
Next ()
}
App.use (middlewareA)
App.use (middlewareB)
App.use (middlewareC)
Http.createServer (app) .clients ('3000girls, function () {
Console.log ('listening 3000.')
});
The above code is the core code of the express middleware mechanism.
To put it simply, there are the following points:
The express function call returns an app instance.
Define an array inside the express function to store the middleware function
Define an app function inside the express function
Define a variable I inside the app function to hold the location of the executed middleware.
Define a next method in the app function, which invokes the middleware by incrementing the I value
Call next inside the app function
Define a use method on the app function, which puts the middleware function push into the middleware array.
In this case, the calling process of express middleware is actually a lot of nesting functions, similar to the following code:
The more middleware, the more levels of nesting.
At this point, the study of "the use, principle and implementation of Express middleware" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.