how to print the data from post request on console

Viewed 97246

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

6 Answers

Use built in function "util" to print any type of json data in express js

var util = require("util");
console.log(util.inspect(myObject, {showHidden: false, depth: null}));
Related