361

我将以下 JSON 字符串发送到我的服务器。

(
        {
        id = 1;
        name = foo;
    },
        {
        id = 2;
        name = bar;
    }
)

在服务器上我有这个。

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

当我发送字符串时,它显示我收到了 200 响应,但其他两种方法从未运行。这是为什么?

4

7 回答 7

624

我认为您将response对象的使用与request.

response对象用于将 HTTP 响应发送回调用客户端,而您想要访问request. 请参阅此答案,它提供了一些指导。

如果您使用的是有效的 JSON 并使用 POST Content-Type: application/json,那么您可以使用bodyParser中间件来解析请求正文并将结果放入request.body您的路由中。

Express 4.16+ 的更新

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

为 Express 4.0 - 4.15 更新

正文解析器在 v4 之后被拆分成自己的 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);

对于 Express 的早期版本 (< 4)

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

沿着以下路线进行测试:

$ curl -d '{"MyKey":"My Value"}' -H "Content-Type: application/json" http://127.0.0.1:3000/
{"MyKey":"My Value"}
于 2012-04-04T08:33:16.283 回答
220

对于 Express v4+

从 npm 安装 body-parser。

$ npm install body-parser

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()
})
于 2014-07-08T15:05:25.977 回答
42

对于那些得到一个空对象的人req.body

我忘了 headers: {"Content-Type": "application/json"} 在请求中设置。改变它解决了这个问题。

于 2019-01-10T03:10:50.720 回答
19

@Daniel Thompson 提到他忘记在 request 中添加 {"Content-Type": "application/json" }。他能够更改请求,但是,更改请求并不总是可能的(我们正在这里的服务器上工作)。

就我而言,我需要强制将 content-type: text/plain 解析为 json。

如果您无法更改请求的内容类型,请尝试使用以下代码:

app.use(express.json({type: '*/*'}));

而不是全局使用 express.json() ,我更喜欢只在需要的地方应用它,例如在 POST 请求中:

app.post('/mypost', express.json({type: '*/*'}), (req, res) => {
  // echo json
  res.json(req.body);
});
于 2019-04-17T10:18:01.200 回答
17

有时您不需要第三方库来解析文本中的 JSON。有时你只需要下面的 JS 命令,先试试吧:

        const res_data = JSON.parse(body);
于 2016-07-21T03:29:17.870 回答
16

const express = require('express');
let app = express();
app.use(express.json());

这个 app.use(express.json) 现在可以让你读取传入的 JSON 对象

于 2019-03-26T19:18:38.583 回答
1

初学者的错误...我app.use(express.json());本地模块而不是主文件(入口点)中使用。

于 2021-02-25T16:19:51.737 回答