Home > Web Front-end > JS Tutorial > body text

Detailed explanation of routing rules in express and how to obtain request parameters

小云云
Release: 2018-03-13 09:26:39
Original
1698 people have browsed it

This article mainly shares with you a method based on routing rules and obtaining request parameters in express. It has a good reference value and I hope it can help everyone.

Common routing rules in express

The main routing rules used are get and post, namely


var express = require('express');
var app = express();
app.get();  // get和post两种请求方式
app.post();
Copy after login

The first parameter of app.get() and app.post() is the request path, and the second parameter is the callback function that handles the request; the callback function has two parameters, Req and res, respectively, represent request information and response information.

Get the request path and various parameters in the request body

The path request and the corresponding form of getting the request path are as follows:

(1) req.query (query the parameters in the get request)


GET /shoes?order=desc&shoe[type]=converse&shoe[color]=blue
req.query.order
// =>'desc'
req,query.shoe.type
// =>'converse'
Copy after login

(2) req.body (query request body)


##

// POST user[name]=dby&user[email]=bing@163.com
req.body.user.name
// =>'dby'
Copy after login

(3) req.params


// GET /file/javascript/jquery.js
req.params[0]
// => 'javascript/jquery.js'
Copy after login

(4) req.params(name)


// ?name=tobi
req.params(name)
// => 'tobi'
// POST name=tobi
req.param('name')
// => 'tobi'
Copy after login
From the above code, we can clearly see the meaning of various acquisition paths:

req.query: Process the get request and obtain the request parameters of the get request

req.params: Process /: For get or post requests in the form of xxx, get the request parameters

req.body: Process the post request, get the request body of the posted request

req.param(): Process the get and post requests, But the search priority from high to low is req.params->req.body->req.query

Note: Path rules support regular expressions.

Related recommendations:


thinkphp routing rule usage examples and pseudo-static function implementation (apache rewriting)_PHP tutorial

thinkphp URL routing rules and configuration examples, thinkphpurl

thinkphp URL routing rules and configuration examples

The above is the detailed content of Detailed explanation of routing rules in express and how to obtain request parameters. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!