Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

express.js - req.body?

I'm new to js, I see this a lot in the code I'm reading

_.pick(req.body, ' ' , ' ') 

What does req.body do? And when can I say req.body.something?

like image 467
User_y Avatar asked Dec 23 '12 03:12

User_y


People also ask

What is req body in Express JS?

The req. body object allows you to access data in a string or JSON object from the client side. You generally use the req. body object to receive data through POST and PUT requests in the Express server.

How do I get a body from Express request?

Express has a built-in express. json() function that returns an Express middleware function that parses JSON HTTP request bodies into JavaScript objects. The json() middleware adds a body property to the Express request req . To access the parsed request body, use req.

Does Express require body parser?

No need to install body-parser with express , but you have to use it if you will receive post request.


2 Answers

req.body holds parameters that are sent up from the client as part of a POST request. See the API.

// POST user[name]=tobi&user[email][email protected]
req.body.user.name
// => "tobi"

req.body.user.email
// => "[email protected]"

// POST { "name": "tobi" }
req.body.name
// => "tobi"
like image 147
TomJ Avatar answered Nov 15 '22 15:11

TomJ


(req.body, ' ' , ' ') --> here req is the parameter of your function and using this parameter your can access the properties over then url.
so look this example
suppose this is form 
<form>
enter the name : <input type="text" name="name">
<button type ="submit"> submit </button> 
</form>

so if you want to access the name -- which is filled by the user end.
so for this you can 
do like this->   console.log(req.body.name);  -- this will print the name (property) in console.
like image 22
Muo sigma classes Avatar answered Nov 15 '22 15:11

Muo sigma classes