Request body contains the http request species, POST, PUT, PATCH three kinds of request methods, the so-called request, the native http Nodejs module, the request based on the flow body to receive and parse manner.
body-parser parsing an HTTP request body middleware , using this module may parse JSON, Raw, text, URL-encoded format request body,
Node native http module, data is encapsulated into a user request for a request req object, the object is a IncomingMessage, the object is also read a stream object. When native Http server does not rely on third parties or parsing module, the following method may request and parses the request body
const http = require('http'); http.createServer(function(req, res){ if(req.method.toLowerCase() === 'post'){ let body = ''; //此步骤为接收数据 req.on('data', function(chunk){ body += chunk; }); //开始解析 req.on('end', function(){ if(req.headers['content-type'].indexOf('application/json')!==-1){ JSON.parse(body); }else if(req.headers [ 'Content-type'] the indexOf ( 'file application / OCTET-Stream') == -.!. 1 ) { // Rwa request body format parsing } the else IF (req.headers [ 'Content-type'] .indexOf ( 'text / Plain') == -!. 1 ) { // text request body text parsing } the else IF (req.headers [ 'Content-type'] the indexOf ( 'file application / X-WWW-form-. ! urlencoded ') == -. 1 ) { // urlencoded format request body parsing } the else { // other format parsing } }) } the else { RES.end('Other mode' ) } }). The listen ( 3000)
Express default frame body-parser parses the request body as middleware, can be found after the creation of the Express project in app.js file
/* 引入依赖项 */ var express = require('express'); // …… var bodyParser = require('body-parser'); var routes = require('./routes/index'); var users = require('./routes/users'); var app = express(); // …… // 解析 application/json app.use(bodyParser.json()); // 解析 application/x-www-form-urlencoded app.use(bodyParser.urlencoded());
This allows the application program level, the introduction of the body-parser module to process the request thereof. In the above code, the processing module application / x-www-form-urlencoded, application / json request body two formats.
After this intermediate, you can access request parameters in all routing processor req.body
In a real project, the different paths may require the user to use different types of content , body-parser also express support for individual routing addition request parsing thereof, such as
Express the require = var ( 'Express' ); var bodyParser = the require ( 'body-Parser' ); var App = new new Express (); // Create application / json parsing var jsonParser = bodyParser.json (); // Create application / X-WWW-form-urlencoded var urlencodedParser = bodyParser.urlencoded ({Extended: to false }); // Get URL-encoded POST / login request body app.post ( '/ Login' , urlencodedParser, function (REQ, RES ) { IF (req.body)! return res.sendStatus (400 ); res.send ( 'available for purchase,' + req.body.username); }) //POST / api / users acquired JSON-encoded request body app.post ( '/ API / Users' , jsonParser, function (REQ, RES) { IF (req.body)! Return res.sendStatus (400 ); // Create User req.body in })
<< request type specifies >>
body-Parser supports the specification of one or analytically for a certain type of content type of the request body, means the timing can be modified by adding a type parameter specifies the Content-Type analytically in the analysis method.
For example, for text / plain content type using JSON parsing
app.use(bodyParser.json({type: 'text/plain'}))
This option is more resolved in a non-standard request header
// parse JSON custom app.use (bodyParser.json ({type: 'file application / JSON * +' })) // parsing custom Buffer app.use (bodyParser.raw ({type: 'file application / VND type-.custom ' })) // the request body as HTML string processing app.use (bodyParser.text ({type:' text / html '}))
<< body-parser module API >>
When the body resolve the request, the analytical value is placed req.body properties, when the time is empty, an empty subject} { --- bodyParser.json () - Parsing JSON format --- bodyParser.raw ( ) - parsing binary format --- bodyParser.text () - parse text format --- bodyParser.urlencoded () - parse text format
Source: https: //www.jianshu.com/p/cd3de110b4b6