如何从控制台上的post请求打印数据

时间:2013-12-13 10:31:48

标签: node.js express

我正在尝试在我的控制台上打印帖子数据


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'));
});

初始快照 ::

enter image description here


我使用POST-MAN测试以下数据 ::

enter image description here


现在我收到错误 ::

enter image description here


  • 我只想打印从邮递员dev收到的数据 .....显示为undefined
  • 如何解决此问题!

[编辑] ----在路径外添加身体解析器

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'));
});

仍然有相同的错误

5 个答案:

答案 0 :(得分:13)

而不是query

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

使用body

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

代码:

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'));
});

答案 1 :(得分:9)

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);

答案 2 :(得分:7)

有查询字符串参数时使用request.query

使用表单/帖子数据req.body

在您的情况下,请使用request.body.key

答案 3 :(得分:1)

您无法在中间件/路由处理程序中调用app.use(express.bodyParser());

  • 请求应在到达路由处理程序之前通过bodyParser()
  • 您将在每个请求中添加新的bodyParser(),但它们将在app.router之后并且永远不会正常工作

答案 4 :(得分:0)

  

使用内置函数“ util”在快速js中打印任何类型的json数据

Combination #1: 25 Paisa x 0 + 50 Paisa x 10 = 5 Rs.
Combination #1: 25 Paisa x 2 + 50 Paisa x 9 = 5 Rs.
Combination #1: 25 Paisa x 4 + 50 Paisa x 8 = 5 Rs.
Combination #1: 25 Paisa x 6 + 50 Paisa x 7 = 5 Rs.
Combination #1: 25 Paisa x 8 + 50 Paisa x 6 = 5 Rs.
Combination #1: 25 Paisa x 10 + 50 Paisa x 5 = 5 Rs.
Combination #1: 25 Paisa x 12 + 50 Paisa x 4 = 5 Rs.
Combination #1: 25 Paisa x 14 + 50 Paisa x 3 = 5 Rs.
Combination #1: 25 Paisa x 16 + 50 Paisa x 2 = 5 Rs.
Combination #1: 25 Paisa x 18 + 50 Paisa x 1 = 5 Rs.
Combination #1: 25 Paisa x 20 + 50 Paisa x 0 = 5 Rs.