node. JS Learning Note 10--express website (2)

Source: Internet
Author: User
Tags representational state transfer

1. Create a Routing rule:

For example, to create a/hello page:

Add in Index.js:

App.get ('/hello ', Funciton (req, res, next) {

Res.send ("The server time is" + New Date (). toString ());

})

App.get is a route rule creation function that accepts two parameters, a route path, and another parameter is a callback function that invokes a callback function when the routing rule is triggered.

2. Path matching

Express also supports more advanced path-matching patterns, such as:

Want to show a user's personal page, the path is/user/[username]

App.get ('/user/:username ', function (req,res) {

Res.send (' User: ' +req.params.username);

})

The routing rules also support JS regular expressions.

3.REST-style routing rules

Rest means: Representational State transfer (representation, Transfer), which is the interface style of the Web application based on the HTTP protocol.

The HTTP protocol defines 8 standard styles:

Among them, get,post,delete,put is commonly used and is characterized by:

The security is that there is no side effect, that is, the request does not change the resource, and the results obtained by successive accesses are not affected by the visitor.

Idempotent means that repeated requests are the same as the effect of a request more than once.

Express has designed different route binding functions for each HTTP request method

Where: The App.all function supports the binding of all request methods to the same response function, which is a very flexible function.

4. Transfer of control rights

Express supports multiple route response functions on the same path, but when accessing any path that is matched to the same two rules, the request is always captured by the previous route rule. ( default is first come first served)

The reason for this is that express, when processing routing rules, prioritizes the first defined routing rules, so that the same rules are masked later.

You can transfer the routing control to the following rule by using the third parameter of the callback function next.

By invoking next (), the routing control is passed to the second rule, which is handed back.

node. JS Learning Note 10--express website (2)

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.