node.js 如何在控制台上打印来自 post 请求的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20564242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:30:21  来源:igfitidea点击:

how to print the data from post request on console

node.jsexpress

提问by Devrath

I am trying to print the post data on my console

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



app.js

应用程序.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::

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

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 !
  • 我只想打印我从邮递员那里收到的数据dev..... 显示为undefined
  • 这个怎么解决!


[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

还是有同样的错误

采纳答案by Shrey

Use request.querywhen you have querystring params.

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

For form/post data use req.body.

对于表单/发布数据,请使用req.body

In your case, use request.body.key.

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

回答by Devrath

Instead of query:

而不是query

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

use body:

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

回答by damphat

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

回答by sumanth

An update on using the middleware, body-parser, for later versions of Express: Using app.use(express.bodyParser())will report an error such as: Error: Most middleware (like bodyParser) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.

使用中间件的更新body-parser,用于更高版本的 Express: Usingapp.use(express.bodyParser())将报告错误,例如: 错误:大多数中间件(如 bodyParser)不再与 Express 捆绑在一起,必须单独安装。请参阅https://github.com/senchalabs/connect#middleware

This can be addressed by first installing the body-parsermiddleware:

这可以通过首先安装body-parser中间件来解决:

npm install body-parser

then write code such as:

然后编写代码,例如:

var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

and then accessing the bodyof the requestobject, for example, console.log(req.body)

然后访问body的的request对象,例如,console.log(req.body)

回答by vinod

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

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

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

回答by vkurchatkin

You can't call app.use(express.bodyParser());inside middleware/route handler:

您不能app.use(express.bodyParser());在中间件/路由处理程序内部调用:

  • request should pass through bodyParser()before it reaches route handler
  • you will be adding new bodyParser()s in each request, but they will be after app.routerand will never work
  • 请求应该bodyParser()在到达路由处理程序之前通过
  • 您将bodyParser()在每个请求中添加 new ,但它们将在之后app.router并且永远不会起作用