本文翻译自:How do I consume the JSON POST data in an Express application
I'm sending the following JSON string to my server. 我正在将以下JSON字符串发送到我的服务器。
(
{
id = 1;
name = foo;
},
{
id = 2;
name = bar;
}
)
On the server I have this. 在服务器上,我有这个。
app.post('/', function(request, response) {
console.log("Got response: " + response.statusCode);
response.on('data', function(chunk) {
queryResponse+=chunk;
console.log('data');
});
response.on('end', function(){
console.log('end');
});
});
When I send the string, it shows that I got a 200 response, but those other two methods never run. 当我发送字符串时,它表明我得到了200的响应,但是其他两种方法却从未运行过。 Why is that? 这是为什么?
#1楼
参考:https://stackoom.com/question/FZ07/如何在Express应用程序中使用JSON-POST数据
#2楼
I think you're conflating the use of the response
object with that of the request
. 我认为您正在将response
对象的使用与request
的使用混为一谈。
The response
object is for sending the HTTP response back to the calling client, whereas you are wanting to access the body of the request
. response
对象用于将HTTP响应发送回调用方客户端,而您希望访问request
的正文。 See this answer which provides some guidance. 请参阅此答案 ,它提供了一些指导。
If you are using valid JSON and are POSTing it with Content-Type: application/json
, then you can use the bodyParser
middleware to parse the request body and place the result in request.body
of your route. 如果您使用有效的JSON并使用Content-Type: application/json
,则可以使用bodyParser
中间件来解析请求正文并将结果放置在路由的request.body
中。
var express = require('express')
, app = express.createServer();
app.use(express.bodyParser());
app.post('/', function(request, response){
console.log(request.body); // your JSON
response.send(request.body); // echo the result back
});
app.listen(3000);
Test along the lines of: 按照以下方式进行测试:
$ curl -d '{"MyKey":"My Value"}' -H "Content-Type: application/json" http://127.0.0.1:3000/
{"MyKey":"My Value"}
Updated for Express 4+ 已针对Express 4+更新
Body parser was split out into it's own npm package after v4, requires a separate install npm install body-parser
v4之后,body解析器被拆分成自己的npm软件包,需要单独安装npm install body-parser
var express = require('express')
, bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.post('/', function(request, response){
console.log(request.body); // your JSON
response.send(request.body); // echo the result back
});
app.listen(3000);
Update for Express 4.16+ Express 4.16+的更新
Starting with release 4.16.0, a new express.json()
middleware is available. 从4.16.0版本开始,可以使用新的express.json()
中间件。
var express = require('express');
var app = express();
app.use(express.json());
app.post('/', function(request, response){
console.log(request.body); // your JSON
response.send(request.body); // echo the result back
});
app.listen(3000);
#3楼
For Express v4+ 对于Express v4 +
install body-parser from the npm. 从npm安装body-parser。
$ npm install body-parser
https://www.npmjs.org/package/body-parser#installation https://www.npmjs.org/package/body-parser#installation
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
// parse application/json
app.use(bodyParser.json())
app.use(function (req, res, next) {
console.log(req.body) // populated!
next()
})
#4楼
Sometimes you don't need third party libraries to parse JSON from text. 有时,您不需要第三方库来解析文本中的JSON。 Sometimes all you need it the following JS command, try it first: 有时,您需要使用以下JS命令,首先尝试它:
const res_data = JSON.parse(body);
#5楼
For those getting an empty object in req.body
对于那些在req.body
获得空对象的req.body
I had forgotten to set headers: {"Content-Type": "application/json"}
in the request. 我忘记了在请求中设置headers: {"Content-Type": "application/json"}
。 Changing it solved the problem. 更改它可以解决问题。
#6楼
const express = require('express'); let app = express(); app.use(express.json());
This app.use(express.json) will now let you read the incoming post JSON object 这个app.use(express.json)现在可以让您读取传入的JSON对象