Home>

routing

Routing refers to how to define the endpoints (uris) of the application and how to respond to client requests.

A route consists of a uri, http request (get, post, etc.) and several handles.Its structure is as follows:app.method (path, [callback ...], callback), app is an instance of express object, method is an http request method, path is the path on the server, callback is the function to execute when the route matches.

Here is a basic routing example:

var express=require ("express");
var app=express ();
//respond with "hello world" when a get request is made to the homepage
app.get ("/", function (req, res) {
 res.send ("hello world");
});

Routing method

The routing method is derived from the http request method,Associated with an express instance.

The following example shows the get and post requests defined for the application and the path:

//get method route
app.get ("/", function (req, res) {
 res.send ("get request to the homepage");
});
//post method route
app.post ("/", function (req, res) {
 res.send ("post request to the homepage");
});

Express defines the following routing methods corresponding to http requests:get, post, put, head, delete, options, trace, copy, lock, mkcol, move, purge, propfind, proppatch, unlock, report, mkactivity, checkout, merge, m-search, notify, subscribe, unsubscribe, patch, search, and connect.

Some routing method names are not compliant javascript variable names, so parentheses are used,For example:app ["m-search"] ("/", function ...

app.all () is a special routing method,No http method corresponds to it.Its role is to load middleware for all requests on a path.

In the following example,Requests from "/secret" will be executed regardless of the use of http requests supported by get, post, put, delete, or any other http module.

app.all ("/secret", function (req, res, next) {
 console.log ("accessing the secret section ...");
 next ();//pass control to the next handler
});

Routing path

The routing path and the request method together define the endpoint of the request,It can be a string, a string pattern, or a regular expression.

The query string is not part of the routing path.

Example routing path using strings:

//request matching root path
app.get ("/", function (req, res) {
 res.send ("root");
});
//request matching/about path
app.get ("/about", function (req, res) {
 res.send ("about");
});
//request matching /random.text path
app.get ("/random.text", function (req, res) {
 res.send ("random.text");
});
Example routing path using string mode:
//match acd and abcd
app.get ("/ab?cd", function (req, res) {
 res.send ("ab?cd");
});
//match abcd, abbcd, abbbcd, etc.
app.get ("/ab + cd", function (req, res) {
 res.send ("ab + cd");
});
//match abcd, abxcd, abrabdomcd, ab123cd, etc.
app.get ("/ab * cd", function (req, res) {
 res.send ("ab * cd");
});
//match/abe and/abcde
app.get ("/ab (cd)?e", function (req, res) {
 res.send ("ab (cd)?e");
});

The characters? ;, +, *, and () are subsets of regular expressions,-And. Are interpreted literally in string-based paths.

Example of a routing path using regular expressions:

//Match any path with a in it:
app.get (/a /, function (req, res) {
 res.send ("/a /");
});
//match butterfly, dragonfly, not butterflyman, dragonfly man, etc.
app.get (/.* fly $/, function (req, res) {
 res.send ("/.* fly $/");
});

Route handle

Can provide multiple callback functions for request processing,It behaves like middleware. The only difference is that these callback functions may call the next ("route") method and skip other route callback functions.You can use this mechanism to define prerequisites for routing,If it doesn't make sense to continue on the existing path,Control can then be passed to the remaining paths.

There are many forms of route handles.Can be a function, an array of functions,Or a mixture of the two,As follows.

Use a callback function to handle routing:

app.get ("/example/a", function (req, res) {
 res.send ("hello from a!");
});

Use multiple callback functions to handle routing (remember to specify the next object):

app.get ("/example/b", function (req, res, next) {
 console.log ("response will be sent by the next function ...");
 next ();
}, function (req, res) {
 res.send ("hello from b!");
});

Handling routes with an array of callback functions:

var cb0=function (req, res, next) {
 console.log ("cb0");
 next ();
}
var cb1=function (req, res, next) {
 console.log ("cb1");
 next ();
}
var cb2=function (req, res) {
 res.send ("hello from c!");
}
app.get ("/example/c", [cb0, cb1, cb2]);

Mixing functions and function arrays for routing:

var cb0=function (req, res, next) {
 console.log ("cb0");
 next ();
}
var cb1=function (req, res, next) {
 console.log ("cb1");
 next ();
}
app.get ("/example/d", [cb0, cb1], function (req, res, next) {
 console.log ("response will be sent by the next function ...");
 next ();
}, function (req, res) {
 res.send ("hello from d!");
});

Response method

The method of the response object (res) in the following table returns a response to the client.End the loop of the request response.If neither method is called in the route handle,Requests from clients will always hang.

  • Previous Simple application of AJAX technology in PHP development
  • Next jQuery manual click to achieve picture carousel effect