In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to realize the middleware of node". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to realize the middleware of node" can help you solve the problem.
In node, middleware is a kind of functional encapsulation, which mainly refers to the method of encapsulating the details of all http requests; http requests usually contain a lot of content, so middleware can be used to simplify and isolate the details between these infrastructure and business logic.
Operating environment of this tutorial: windows10 system, nodejs version 12.19.0, Dell G3 computer.
What is the middleware of node?
Nodejs middleware is conceptually a way of encapsulating functions, which mainly refers to the method of encapsulating the details of all Http requests.
In nodeJS, middleware mainly refers to the method that encapsulates the details of all Http requests. A Http request usually contains a lot of work, such as logging, IP filtering, query string, request body parsing, cookie processing, permission verification, parameter verification, exception handling, etc., but for web applications, we do not want to be exposed to so many detailed processing. Therefore, using middleware to simplify and isolate the details between these infrastructure and business logic, so that developers pay more attention to business development, his working model is as follows:
Core implementation of middleware mechanism
Middleware is the processing method from the initiation of the Http request to the end of the response, which usually requires the processing of the request and response, so a basic middleware takes the following form:
Const middleware = (req, res, next) = > {/ / TODO next ()} simulate the most basic middleware / / simply define three middleware const httpMeth2 = (req, res, next) = > {console.log ('I am request 1') next ()} const httpMeth3 = (req, res) Next) = > {console.log ('I am request 2') next ()} const httpMeth4 = (req, res, next) = > {console.log ('I am request 3') next ()} / / Middleware array const allHttpMeth = [httpMeth2, httpMeth3, httpMeth4] function run (req Res) {const next = () = > {/ / get middleware const allHttpMethitem = allHttpMeth.shift () if (allHttpMethitem) {/ / execute allHttpMethitem (req, res, next)}} next ()} run () / / initiate a simulation request
If there is an asynchronous operation in the middleware, the next () method needs to be called after the flow of the asynchronous operation ends, otherwise the middleware cannot be executed sequentially.
This is the end of the introduction of "how to implement the middleware of node". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.