Express using middleware

Source: Internet
Author: User
Tags error handling http request

From: http://www.expressjs.com.cn/guide/using-middleware.html#middleware.application

Express is a minimalist web development framework that is entirely composed of routing and middleware: Essentially, an express application is invoking a variety of middleware.

The Middleware (middleware) is a function that accesses the request object (req), the Response object (Response object (res)), and the middleware in the Web application that is in the request-response loop process. A variable that is generally named next.

The middleware features include: Execute any code. Modifies the request and response objects. The end request-response loop. The next middleware in the call stack.

If the current middleware does not have an end request-response loop, the next () method must be called to give control to the next middleware or the request will be suspended.

The following middleware can be used for Express applications: application-level middleware routing-level middleware error handling middleware built-in middleware third-party middleware

Use the optional mount path to load the middleware at the application level or at the route level. In addition, you can also install a series of middleware functions at the same time to create a sub-middleware stack on a mount point. application-level middleware

App-level middleware binds to app objects using App.use () and apps. Method (), where methods are required to process HTTP requests, such as GET, PUT, POST, and so on, all lowercase. For example:

var app = Express ();

Middleware without Mount path, each request of the application executes the middleware
app.use (function (req, res, next) {
  Console.log (' Time: ', Date.now ());
  Next ();
});

The middleware attached to/user/:id, any request to/user/:id will execute it
app.use ('/user/:id ', function (req, res, next) {
  Console.log (' Request Type: ', req.method);
  Next ();
});

The Routing and Handle function (middleware system) that handles the GET request to the/user/:id
app.get ('/user/:id ', function (req, res, next) {
  res.send (' user '); c13/>});

The following example shows the loading of a set of middleware at a mount point.

A middleware stack that prints information about any HTTP request that points to/user/:id
app.use ('/user/:id ', function (req, res, next) {
  Console.log (' Request URL: ', req.originalurl);
  Next ();
}, Function (req, res, next) {
  console.log (' Request Type: ', req.method);
  Next ();
});

As a routing handle to a middleware system, it makes it possible to define multiple routes for a path. In the following example, two routes are defined for GET requests that point to/user/:id. The second route, while not causing any problems, will never be called because the first route has terminated the request-response loop.

A middleware stack that handles GET requests to/user/:id
app.get ('/user/:id ', function (req, res, next) {
  console.log (' ID: ', req.params. ID);
  Next ();
}, Function (req, res, next) {
  res.send (' User Info ');
});

Process/user/:id, print out User ID
app.get ('/user/:id ', function (req, res, next) {
  res.end (req.params.id);
});

If you need to skip the remaining middleware in the middleware stack, call the next (' route ') method to pass control over to the next route. Note: Next (' Route ') only uses the app. VERB () or router. VERB () loaded middleware is valid.

A middleware stack that handles GET requests
to/user/:id app.get ('/user/:id ', function (req, res, next) {
  //If the user ID is 0, skip to the next route
  if (req.params.id = = 0) Next (' Route ');
  Otherwise, the control is given to the next middleware in the stack
  else next ();//
}, Function (req, res, next) {
  //Render General page
  Res.render (' regular ');
} );

Processing/user/:id, rendering a special page
app.get ('/user/:id ', function (req, res, next) {
  res.render (' Special ');
});
routing-level middleware

Routing-level middleware is just like application-level middleware, except that it binds to express. Router ().

var router = Express. Router ();

The routing level uses router.use () or router. VERB () load.

The above middleware systems created at the application level can be rewritten to the routing level by the following code:

var app = Express (); var router = Express.

Router ();
  Middleware with no mount path, each request through that route executes the middleware router.use (function (req, res, next) {Console.log (' time: ', Date.now ()); Next (

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.