Express Route detail_javascript skills

Source: Internet
Author: User
This article mainly introduces detailed information about Express routes. For more information, see Routing

Routing refers to how to define the application endpoint (URIs) 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), the app is an instance of the express object, the METHOD is an HTTP request METHOD, the path is the path on the server, and the callback is the function to be executed when the route matches.

The following is a basic routing example:

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

Routing Method

The routing method is derived from the HTTP Request Method and associated with the express instance.

The following example shows the GET and POST requests defined for the application and Path:

// GET method routeapp.get('/', function (req, res) { res.send('GET request to the homepage');});// POST method routeapp.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, proppatch, unlock, report, mkactivity, checkout, merge, m-search, sort y, subscribe, unsubscribe, patch, search, and connect.

Some routing method names are not compliant JavaScript variable names. In this case, brackets are used, for example, app ['m-search'] ('/', function...
App. all () is a special routing method. No HTTP method corresponds to it. It loads middleware for all requests in a path.

In the following example, a request from "/secret" is 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});

Route path

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

Express uses path-to-regexp to match the route path. For more information about how to define the route path, see. Express Route Tester is a good tool for testing basic Express paths, but pattern matching is not supported.
The query string is not part of the route path.

Example of a routing path using a string:

// Request app that matches the root path. get ('/', function (req, res) {res. send ('root') ;}); // request app that matches the/about path. get ('/about', function (req, res) {res. send ('about') ;}); // match/random. text path request app. get ('/random. text', function (req, res) {res. send ('random. text ');}); example of a routing path in string mode: // match acd and abcdapp. get ('/AB? Cd', function (req, res) {res. send ('AB? Cd ');}); // matches apps such as abcd, abbcd, and abbbcd. get ('/AB + Cd', function (req, res) {res. send ('AB + Cd') ;}); // matches apps such as abcd, abxcd, abRABDOMcd, and ab123cd. get ('/AB * Cd', function (req, res) {res. send ('AB * Cd') ;}); // match/abe and/abcdeapp. get ('/AB (cd )? E ', function (req, res) {res. send (' AB (cd )? E ');});

Character? , +, *, And () are the subsets of regular expressions.-And. are interpreted according to the nominal value in the string-based path.
Example of a routing path using a regular expression:

// Match the path containing a in any path: app. get (/a/, function (req, res) {res. send ('/a/') ;}); // matches butterfly, dragonfly, and does not match apps such as butterflyman and dragonfly man. get (/. * fly $/, function (req, res) {res. send ('/. * fly $ /');});

Route handle

Multiple callback functions can be provided for request processing, and their behavior is similar to that of 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 preconditions for a route. If it makes no sense to continue executing on an existing path, you can give control to the remaining path.

The routing handle can be a function, a function array, or a combination 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 routes (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!');});

Use the callback function array to process routes:

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]);

Use a combination 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 will be sent by the next function ...'); next();}, function (req, res) { res.send('Hello from D!');});

Response Method

In the following table, the response object (res) method returns a response to the client to terminate the request response cycle. If a method in the route handle is not called, requests from the client will be suspended all the time.

Related Article

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.