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)