Home  >  Article  >  Web Front-end  >  A brief discussion on several methods of obtaining parameters in Nodejs

A brief discussion on several methods of obtaining parameters in Nodejs

青灯夜游
青灯夜游forward
2021-01-21 17:10:322828browse

How does Nodejs get the parameters in the request? The following article will introduce to you the four methods of Nodejs to obtain parameters. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

A brief discussion on several methods of obtaining parameters in Nodejs

Related recommendations: "nodejs Tutorial"

Four ways to get parameters in NodeJs

Reference: https://my.oschina.net/u/2519530/blog/535309

Getting the parameters in the request is the only way for every web background processing, nodejs The express framework provides four methods to achieve this.

  • req.body

  • req.query

  • req.params

  • req.param()

First introduce the first req.body

Official document explanation :
Contains key-value pairs of data submitted in the request body. By default, it is undefined,
and is populated when you use body-parsing middleware such as body-parser and multer.

A little translation: The key-value pair containing the submitted data is in the body of the request. The default is underfined.
You can use body-parser or multer to parse the body

Parsing the body is not nodejs Provided by default, you need to load the body-parser middleware before you can use req.body

This method is usually used to parse the data in the POST request

The second is req.query

Official document explanation:

An object containing a property for each query string parameter in the route.
If there is no query string, it is the empty object, {}.

Translation: An object containing the properties of each query string parameter in the route. If not, the default is {}

Nodejs provides it by default, no need to load middleware

Example (official excerpt):

// GET /search?q=tobi+ferret
req.query.q
// => "tobi ferret"

// GET /shoes?order=desc&shoe[color]=blue&shoe[type]=converse
req.query.order
// => "desc"
req.query.shoe.color
// => "blue"
req.query.shoe.type
// => "converse"

This method is mostly suitable for GET requests, parsing the parameters in GET

The third method is req.params

Official documentation:
An object containing properties mapped to the named route “parameters”.
For example, if you have the route /user/:name,
then the “name” property is available as req.params.name. This object defaults to {}.

Translation: An object containing the "params" property mapped to the specified route.
For example, if you have route/user/:name, then the "name" attribute is available as req.params.name.
The object defaults to {}.

Nodejs provides it by default, no need to load other middleware

Examples

// GET /user/tj
req.params.name
// => "tj"

Mostly suitable for restful style URLs Parsing of parameters

The difference between req.query and req.params

req.params contains routing parameters (in the path part of the URL) , and req.query contains the query parameters of the URL (the parameters after the ? in the URL).

The last req.param()

This method is deprecated, please see the official explanation

Deprecated. Use either req.params, req.body or req.query, as applicable.
Translation: Deprecated, replaced by three other ways

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of A brief discussion on several methods of obtaining parameters in Nodejs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete