node. js

Source: Internet
Author: User

1. Routers

Routing is how you define an app's endpoints (URIs) and how to respond to requests from clients.

A route consists of a URI, an HTTP request (GET, post, and so on) and a number of handles, which are structured as follows: an instance of an object, an app.METHOD(path, [callback...], callback) app express METHOD http request method, a path path on a server, a callback route The function to execute when matching.

Here is a basic example of a route:

var express =require(‘express‘);var app =express();// respond with "hello world" when a GET request is made to the homepageapp.get(‘/‘,function(req, res){  res.send(‘hello world‘);});
Routing Methods 2. Middleware

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 can access the request object (requests ( req )), the Response object (Response object ( res )), and the Web The middleware in the application that is in the request-response loop process is generally named as next a variable.

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 method must be called to next() give control to the next middleware or the request will be suspended.

There are several types of middleware that Express applications can use:

    • 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.

node. js

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: 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.