Devrath Devrath - 4 months ago 11
Node.js Question

how to print the data from post request on console

I am trying to print the post data on my console






app.js

var express = require('express')
, http = require('http');

var app = express();

app.set('port', process.env.PORT || 7002);

app.use(express.static(__dirname + '/public/images'));


app.post('/Details/',function(request,response,next){


app.use(express.bodyParser());

var keyName=request.query.Key;
console.log(keyName);

} );


http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});





Initial snapshot::



enter image description here




I test with POST-MAN with below data::



enter image description here




Now i get error as::



enter image description here





  • I just want to print the data i recieved from postman that is
    dev

    ..... which is being displayed as
    undefined
    ?

  • How to resolve this !






[Edit] ---- Adding body parser outside the route



var express = require('express')
, http = require('http');

var app = express();

app.set('port', process.env.PORT || 7002);

app.use(express.static(__dirname + '/public/images'));

app.use(express.bodyParser());

app.post('/Details/',function(request,response,next){

var keyName=request.query.Key;
console.log(keyName);

} );


http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});





Still have same error


Answer

Use request.query when you have querystring params.

For form/post data use req.body.

In your case, use request.body.key.

Comments