In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the middleware example analysis of node Express". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "node Express middleware example analysis" bar!
Express is a concise and flexible Web application development framework, which can be used to quickly build a fully functional Web application. As an upper service framework based on Node.js encapsulation, Express provides a more concise API, which makes it easier to organize and manage applications through middleware and routing.
The concept of middleware
Middleware is a number of sub-processing functions that modularize the request processing function, and some column sub-processing functions can form a middleware stack.
Middleware is a function that can access the request object req, response object res and next () function in the request-response cycle of the application. The next () function is mainly responsible for handing control to the next middleware; if the current middleware does not terminate the request and next () is not called, then the request will be suspended and the middleware defined later will not be executed. 、
The execution order of middleware is matched from top to bottom in strict accordance with the order of registration.
Middleware functions that can perform the following tasks:
Execute any code
Modify request and response objects
End the request-response cycle
The next middleware in the call stack (next)
The main purpose of middleware is to deal with HTTP requests, which is used to complete specific tasks such as login status verification, request log, error handling, Cookie and so on.
Middleware classification
1 application-level middleware
Use the app.use () function to bind application-level middleware to an application object instance
Const app = express (); / * indicates matching any route * / app.use (function (req,res,next) {console.log ('request time:' + Date.now ()); / * indicates that the matching completes and the middleware continues to execute. * / next ()})
2 routing-level middleware
Routing-level middleware works the same way as application-level middleware, except that it is bound to the router instance
Import express from 'express';const app = express (); const router = express.router (); router.use (' / user', function (req, res, next) {console.log (1); next ();}, function (req, res, next) {console.log (2); next ();}, function (rex, res, next) {console.log (3); next ();})
3 error handling middleware
Error handling middleware always requires four parameters, and four parameters must be provided to identify it as an error handling middleware function. The next function must be specified even if it is not required. Otherwise, the next function is interpreted as regular middleware and cannot handle errors
App.use (function (err, req, res, next) {console.log (err.stack); res.status (500) .send (err);})
4 built-in middleware
Express has the following built-in middleware:
Express.static: provide static resource service
Express.router: provide routing service
5 third-party middleware
Third-party middleware such as body-parser, cookie-parser, etc.
6 Custom Middleware
Custom middleware is defined as a function that accepts the req,res,next parameter and registers the middleware using app.use ()
Function log (req,res,next) {req.requestTime = Date.now (); next ()} / / register custom middleware app.use (log) / / Custom configurable middleware function log (options) {return function (req,res,next) {/ / according to options to achieve middleware function next}} so far, I believe you have a deeper understanding of "node Express middleware example analysis", you might as well to practice it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.