If there is a novice to learn nodejs , get url post The same problem will occur when the parameter is returned. Let's solve this small problem today

node.js No php This old back-end language has specific methods to obtain external parameters. After all, it is a new force. I hope the later version can be more convenient and efficient

 //Normal acquisition values 
 app. Get ('/', (req, RES) = > {console. Log (req. Query); 
 res.send ('complete '); 
 app. Post (' / '(req, RES) = > {console. Log (req. Body); 
 res.send ('complete'); 

Normal output, your expectation is like this

 //Get request 
 {Name: 'Feng Xiaoxian', 
 age: '18' 
} completed


 //'post: {18}

But it's not!!!

 //Twice the output will be like this!!! 
{ }

This is because we lack a module, express middleware body-parser

Official statement
Node.js body parsing middleware.
Parse incoming request bodies in a middleware before your handlers, available under the req.body property.

Basically, parsing the content in the body of the client request, JSON encoding processing and URL encoding processing

Next we'll install it body-parser

 //Install body parser
npm install body parser

 //Import app.js/var varbodyparser = require ('body-parser '); app.use (bodyparser());;; 
 / / / so you can use req.body to get the value passed by post, using req.body to get the value passed by post. App.post (' / ', (req, RES) = > {app.post (' / ', (req, RES) = > > {app.post (' / ', (req, res, RES) = > {console.log (req.body); 
res.send ('complete'); 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;. Json())

label: nodejs , post , express

Add a new comment