Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to print the data from post request on console

Tags:

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

like image 430
Devrath Avatar asked Dec 13 '13 10:12

Devrath


People also ask

Is console log same as print?

The only notable difference between the two is that, when printing an object, console. log gives special treatment to HTML elements, while console.

How do I print a console log without a new line?

stdout. write() method to print to console without trailing newline. Note: The process object is global so it can be used without using require() method.

What is a get request in Express?

GET requests are used to send only limited amount of data because data is sent into header while POST requests are used to send large amount of data because data is sent in the body. Express. js facilitates you to handle GET and POST requests using the instance of express.


2 Answers

Instead of query:

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

use body:

var keyName1=request.body.key; console.log(keyName1); 

Code:

var express = require('express')  , async = require('async')  , 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 keyName1=request.body.key;    console.log(keyName1); } );   http.createServer(app).listen(app.get('port'), function(){  console.log('Express server listening on port ' + app.get('port')); }); 
like image 196
Devrath Avatar answered Oct 07 '22 09:10

Devrath


var express = require('express'); var app = express();  app.use(express.bodyParser());  app.post('/post/', function(req, res) {    // print to console    console.log(req.body);     // just call res.end(), or show as string on web    res.send(JSON.stringify(req.body, null, 4)); });  app.listen(7002); 
like image 21
damphat Avatar answered Oct 07 '22 09:10

damphat