SoFunction
Updated on 2025-04-05

Based on routing rules in Express and method of obtaining request parameters

Common routing rules in express

The main routing rules are get and post, i.e.

var express = require('express');
var app = express();
();  // Get and post two request methods();

The first parameter of () and () is the request path, and the second parameter is the callback function that handles the request; the callback function has two parameters, namely req and res, representing the request information and response information.

Get various parameters in the request path and request body

There are several forms of path request and corresponding acquisition request paths:

(1) (Query the parameters in the get request)

GET /shoes?order=desc&shoe[type]=converse&shoe[color]=blue

// =>'desc'
req,
// =>'converse'

(2) (Query request body)

// POST user[name]=dby&user[email]=bing@

// =>'dby'

(3)

// GET /file/javascript/
[0]
// => 'javascript/'

(4)(name)

// ?name=tobi
(name)
// => 'tobi'
// POST name=tobi
('name')
// => 'tobi'

From the above code, we can clearly see the meaning of various acquisition paths:

: Process get request and get the request parameters of the get request

: Processing /:xxx form get or post request to obtain request parameters

: Process the post request and obtain the request body of the post request

(): Process get and post requests, but the search priority is from high to low ->->

Note: Path rules support regular expressions.

The above article is based on routing rules and the method of obtaining request parameters in Express. I hope it can give you a reference and I hope you can support me more.