javascript tutorial - [Solved-5 Solutions] POST query parameters - javascript - java script - javascript array



Problem:

How to retrieve POST query parameters ?

Solution 1:

Things have changed again in Express 4.0:

$ npm install --save body-parser
click below button to copy the code. By JavaScript tutorial team

and then:

var bodyParser = require('body-parser')
app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true
})); 
click below button to copy the code. By JavaScript tutorial team

The rest is like in Express 3.0:

  • Firstly we need to add some middleware to parse the post data of the body.
  • Add one or both of the following lines of code:
app.use(express.json());       // to support JSON-encoded bodies
app.use(express.urlencoded()); // to support URL-encoded bodies
click below button to copy the code. By JavaScript tutorial team

Then, in our handler, use the req.body object:

// assuming POST: name=foo&color=red            <-- URL encoding
//
// or       POST: {"name":"foo","color":"red"}  <-- JSON encoding

app.post('/test-page', function(req, res) {
    var name = req.body.name,
        color = req.body.color;
    // ...
});
click below button to copy the code. By JavaScript tutorial team

Note that the use of express.bodyParser() is not recommended.

app.use(express.bodyParser());
click below button to copy the code. By JavaScript tutorial team

...is equivalent to:

app.use(express.json());
app.use(express.urlencoded());
app.use(express.multipart());
click below button to copy the code. By JavaScript tutorial team

Security concerns exist with express.multipart(), and so it is better to explicitly add support for the specific encoding type(s) we require. If we do need multipart encoding (to support uploading files for example) then we should read this https://groups.google.com/forum/.

Solution 2:

This will do it if we want to build the posted query without middleware:

app.post("/register/",function(req,res){
    var bodyStr = '';
    req.on("data",function(chunk){
        bodyStr += chunk.toString();
    });
    req.on("end",function(){
        res.send(bodyStr);
    });

});
click below button to copy the code. By JavaScript tutorial team

That will send this to the browser

email=emailval&password1=pass1val&password2=pass2val
click below button to copy the code. By JavaScript tutorial team

It's probably better to use middleware though so we don't have to write this over and over in each route.

Solution 3:

Note for Express 4 users:

If we try and put app.use(express.bodyParser()); into our app, you'll get the following error when we try to start our Express server:

Error: Most middleware (like bodyParser) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware .

You'll have to install the package body-parser separately from npm , then use something like the following:

var express    = require('express');
var bodyParser = require('body-parser');

var app = express();

app.use(bodyParser());

app.use(function (req, res, next) {
  console.log(req.body) // populated!
  next();
})
click below button to copy the code. By JavaScript tutorial team

Solution 4:

Given some form:

<form action='/somepath' method='post'>
   <input type='text' name='name'></input>
</form>
click below button to copy the code. By JavaScript tutorial team

Using express

app.post('/somepath', function(req, res) {

    console.log(JSON.stringify(req.body));

    console.log('req.body.name', req.body['name']);
});
click below button to copy the code. By JavaScript tutorial team

Output:

{"name":"x","description":"x"}
req.param.name x

click below button to copy the code. By JavaScript tutorial team

Solution 5:

	app.use(express.bodyParser());
click below button to copy the code. By JavaScript tutorial team

Then for app.post request we can get post values via req.body.{post request variable}.


Related Searches to javascript tutorial - POST query parameters