Routing
Routing is how to define an application's endpoint (URIs) and how to respond to a client's request.
A route consists of a URI, an HTTP request (get, post, and so on) and a number of handles, which are structured as follows: App. Method (path, [callback ...], callback), the app is an instance of the Express object, methods is an HTTP request approach, path is the server, and callback is the function to execute when the route matches.
The following is an example of a basic route:
var express = require (' Express ');
var app = Express ();
Respond with "Hello World" when a GET request was made to the homepage
app.get ('/', function (req, res) {
Res.sen D (' Hello World ');
};
Routing Methods
The routing method originates from the HTTP request method and is associated with the Express instance.
The following example shows the GET and POST requests defined for the application and 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 for HTTP requests: Get, post, put, head, delete, options, trace, copy, lock, MKCOL, move, purge, PROPFIND, pro Ppatch, Unlock, mkactivity, checkout, merge, M-search, notify, subscribe, unsubscribe, patch, search, and connect.
Some routing method names are not compliant JavaScript variable names, and parentheses are used at this time, such as: app[' M-search '] ('/', function ...).
App.all () is a special routing method that does not correspond to any HTTP method, and its role is to load the middleware for all requests on a path.
In the following example, a request from "/secret" will be executed regardless of the HTTP request 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 requested endpoint, which can be a string, a string pattern, or a regular expression.
Express uses Path-to-regexp to match the routing path, refer to the documentation for all methods that define the routing path. Express Route Tester is a good tool for testing basic Express paths, but does not support pattern matching.
The query string is not part of the routing path.
Examples of routing paths that use strings:
The request to match the root path
app.get ('/', function (req, res) {
res.send (' root ');
});
The request to match the/about path
app.get ('/about ', function (req, res) {
res.send (' about ');
});
A request to match the/random.text path
app.get ('/random.text ', function (req, res) {
res.send (' Random.text ');
});
Examples of routing paths using string patterns:
//Matching 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
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 ');
});
Characters?, +, *, and () are subsets of regular expressions-and. Interpreted according to the literal value in a string based path.
Examples of routing paths that use regular expressions:
Matches the path containing a in any path:
app.get (/a/, function (req, res) {
res.send ('/a/');
});
Match butterfly, Dragonfly, mismatched Butterflyman, Dragonfly man
app.get (/.*fly$/, function (req, res) {
res.send ('/. *fly$/');
Route handle
You can provide multiple callback functions for request processing, which behave like middleware. The only difference is that it is possible for these callback functions to invoke the next (' Route ') method and skip over the other routing callback functions. You can use this mechanism to define a prerequisite for a route, and you can give control to the remaining path if it does not make sense to continue execution on the existing path.
There are several forms of a route handle, either a function, an array of functions, or a mixture of the two, as shown below.
Use a callback function to process a route:
App.get ('/example/a ', function (req, res) {
res.send (' Hello from a! ');
});
Use multiple callback functions to process a route (remember to specify a next object):
App.get ('/example/b ', function (req, res, next) {
Console.log (' response'll be sent by the next function ... ');
next ();
}, Function (req, res) {
res.send (' Hello from b! ');
});
To process a route using 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]);
Mixed use of functions and function arrays to process routes:
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'll be sent by the next function ...');
Next ();
}, Function (req, res) {
res.send (' Hello from d! ');
});
Response method
The Response object (res) method in the following table returns a response to the client, terminating the loop of the request response. If a method is not invoked in the route handle, the request from the client is suspended.