node.js - What is the use of body-parser? -


var express = require('express'); var app = express(); var bodyparser = require('body-parser');  // create application/x-www-form-urlencoded parser var urlencodedparser = bodyparser.urlencoded({ extended: false })  app.use(express.static('public'));  app.get('/index.htm', function (req, res) {    res.sendfile( __dirname + "/" + "index.htm" ); })  app.post('/process_post', urlencodedparser, function (req, res) {     // prepare output in json format    response = {        first_name:req.body.first_name,        last_name:req.body.last_name    };    console.log(response);    res.end(json.stringify(response)); })  var server = app.listen(8081, function () {    var host = server.address().address   var port = server.address().port    console.log("example app listening @ http://%s:%s", host, port)  }) 
  1. var bodyparser = require('body-parser');
  2. var urlencodedparser = bodyparser.urlencoded({ extended: false })

  3. app.post('/process_post', urlencodedparser, function (req, res)

can please explain purpose of above 3 lines of code , use of body-parser?

  1. the bodyparser populate req.body property parsed body request. line 1 1 use in class via require.

  2. returns middleware parses urlencoded bodies. parser accepts utf-8 encoding of body , supports automatic inflation of gzip , deflate encodings. extended option allows choose between parsing url-encoded data querystring library (when false) or qs library (when true). "extended" syntax allows rich objects , arrays encoded url-encoded format, allowing json-like experience url-encoded.

  3. with last line saying route has defined use bodyparser have defined

the of stuff have copied https://github.com/expressjs/body-parser. may take @ link


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -