Skip to content

Commit bcf6c33

Browse files
committed
body-parser
1 parent dccce7b commit bcf6c33

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2543,7 +2543,47 @@ app.listen(3000, function(){
25432543
<b><a href="#">↥ back to top</a></b>
25442544
</div>
25452545

2546-
#### Q. ***Explain the terms body-parser, cookie-parser, debug, jade, morgan, nodemon, pm2, serve-favicon, cors, .env, checksum, fs-extra, moment in Express JS?***
2546+
## Q. ***Explain the terms body-parser, cookie-parser, debug, jade, morgan, nodemon, pm2, serve-favicon, cors, .env, checksum, fs-extra, moment in Express JS?***
2547+
2548+
**1. `body-parser`**
2549+
2550+
`body-parser` extract the entire body portion of an incoming request stream and exposes it on `req.body`. This body-parser module parses the JSON, buffer, string and URL encoded data submitted using HTTP POST request.
2551+
2552+
*Example*:
2553+
2554+
```bash
2555+
npm install express ejs body-parser
2556+
```
2557+
2558+
**server.js**
2559+
2560+
```js
2561+
var express = require('express')
2562+
var bodyParser = require('body-parser')
2563+
2564+
var app = express()
2565+
2566+
// create application/json parser
2567+
var jsonParser = bodyParser.json()
2568+
2569+
// create application/x-www-form-urlencoded parser
2570+
var urlencodedParser = bodyParser.urlencoded({ extended: false })
2571+
2572+
// POST /login gets urlencoded bodies
2573+
app.post('/login', urlencodedParser, function (req, res) {
2574+
res.send('welcome, ' + req.body.username)
2575+
})
2576+
2577+
// POST /api/users gets JSON bodies
2578+
app.post('/api/users', jsonParser, function (req, res) {
2579+
// create user in req.body
2580+
})
2581+
```
2582+
2583+
<div align="right">
2584+
<b><a href="#">↥ back to top</a></b>
2585+
</div>
2586+
25472587
#### Q. ***How does routing work in Node.js?***
25482588
#### Q. ***How Node prevents blocking code?***
25492589
#### Q. ***What is difference between promise and async await in Node.js?***

0 commit comments

Comments
 (0)