body-parser using

Node.js. recently come into contact with in the process of learning project Learning process, and middleware express some grammar framework is not very clear. For example, body-parser. Why use it? What does it do? This came on the body-parser to do some notes.

bodyParser Middleware

bodyParser intermediate body used to parse the http request, one of the middleware is used by default express. So the first thing is to introduce bodyParser

 

 

 Use express generate a site that has been used to resolve the default function bodyParser.json and bodyParser.urlencoded, in addition to these two, bodyParser also supports parsing of text, raw's.

 

 

 So the name suggests, bodyParser.json is used to parse the json data format. bodyParser.urlencoded analysis data is used to form our usual form is submitted, that is the request header contains information:

 Content-Type: application/x-www-form-urlencoded

Four common types Content-Type:

  • application / x-www-form-urlencoded common form submission
  • multipart / form-data submissions
  • application / json json format of the data submitted
  • text / xml submit data in xml format

Detailed interpretation urlencoded

bodyParser.urlencoded req.body means for parsing the data, overwriting the original analytical req.body after successful. If the failure was resolved {}. The module has a property extended, officially described as follows:

The extended option allows to choose between parsing the URL-encoded data with the querystring library (when false) or the qs library (when true).

Defaults to true, but using the default has been deprecated.

Roughly meaning: extended option allows you to configure the use querystring (false) or qs (true) to parse the data, the default value is true, but this is not a favor.

querystring is one nodejs built object to parse the string or character string objects. Such as

querystring.parse("name=henry&age=30") => { name: 'henry', age: '30' }

 

Guess you like

Origin www.cnblogs.com/lovekiku123/p/11961094.html
Recommended