Home > Web Front-end > JS Tutorial > Detailed explanation of Express routing_javascript skills

Detailed explanation of Express routing_javascript skills

WBOY
Release: 2016-05-16 15:26:28
Original
1441 people have browsed it

Routing

Routing refers to how to define the application’s endpoints (URIs) and how to respond to client requests.

Routing is composed of a URI, HTTP request (GET, POST, etc.) and several handles. Its structure is as follows: app.METHOD(path, [callback...], callback), app is an express object Example, METHOD is an HTTP request method, path is the path on the server, and callback is the function to be executed when the route is matched.

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');
});
Copy after login

Routing method

The routing method originates from the HTTP request method and is associated with the express instance.

The following example shows 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');
});
Copy after login

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 legal JavaScript variable names. In this case, use bracket notation, for example: app['m-search']('/', function ...
app.all() is a special routing method that does not correspond to any HTTP method. Its function is to load middleware for all requests on a path.

In the example below, the handle will be executed for requests from "/secret" regardless of whether they use GET, POST, PUT, DELETE or any other HTTP request supported by the http module.

app.all('/secret', function (req, res, next) {
 console.log('Accessing the secret section ...');
 next(); // pass control to the next handler
});
Copy after login

Routing path

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

Express uses path-to-regexp to match routing paths. Please refer to the documentation for all methods of defining routing paths. Express Route Tester is a great tool for testing basic Express routes, but does not support pattern matching.
The query string is not part of the routing path.

Example of routing path using string:

// 匹配根路径的请求
app.get('/', function (req, res) {
 res.send('root');
});
// 匹配 /about 路径的请求
app.get('/about', function (req, res) {
 res.send('about');
});
// 匹配 /random.text 路径的请求
app.get('/random.text', function (req, res) {
 res.send('random.text');
});
使用字符串模式的路由路径示例:
// 匹配 acd 和 abcd
app.get('/ab?cd', function(req, res) {
 res.send('ab?cd');
});
// 匹配 abcd、abbcd、abbbcd等
app.get('/ab+cd', function(req, res) {
 res.send('ab+cd');
});
// 匹配 abcd、abxcd、abRABDOMcd、ab123cd等
app.get('/ab*cd', function(req, res) {
 res.send('ab*cd');
});
// 匹配 /abe 和 /abcde
app.get('/ab(cd)?e', function(req, res) {
 res.send('ab(cd)?e');
});
Copy after login

The characters ?, +, * and () are a subset of regular expressions, - and . are interpreted literally in string-based paths.
Example of routing path using regular expressions:

// 匹配任何路径中含有 a 的路径:
app.get(/a/, function(req, res) {
 res.send('/a/');
});
// 匹配 butterfly、dragonfly,不匹配 butterflyman、dragonfly man等
app.get(/.*fly$/, function(req, res) {
 res.send('/.*fly$/');
});
Copy after login

Route handle

You can provide multiple callback functions for request processing, which behave like middleware. The only difference is that these callback functions may call the next('route') method and skip other route callback functions. This mechanism can be used to define preconditions for routing, and if it makes no sense to continue execution on the existing path, control can be given to the remaining path.

Route handles come in many forms, they can be a function, an array of functions, or a mixture of the two, as shown below.

Use a callback function to handle routing:

app.get('/example/a', function (req, res) {
 res.send('Hello from A!');
});
Copy after login

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!');
});
Copy after login

Use callback function array to handle routing:

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]);
Copy after login

Use a mix of functions and function arrays to handle 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!');
});
Copy after login

Response method

The methods of the response object (res) in the following table return a response to the client, terminating the request-response cycle. If no method is called in the route handle, requests from the client will hang.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template