如何在 Express 应用程序中使用 JSON POST 数据 [英] How do I consume the JSON POST data in an Express application

查看:31
本文介绍了如何在 Express 应用程序中使用 JSON POST 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm sending the following JSON string to my server.

(
        {
        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 响应,但其他两种方法从未运行.这是为什么?

When I send the string, it shows that I got a 200 response, but those other two methods never run. Why is that?

推荐答案

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

I think you're conflating the use of the response object with that of the request.

response 对象用于将 HTTP 响应发送回调用客户端,而您想要访问 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. See this answer which provides some guidance.

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

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.

Express 4.16+ 的更新

从 4.16.0 版开始,一个新的 express.json() 中间件可用.

Starting with release 4.16.0, a new express.json() middleware is available.

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 进行了更新

body parser 在 v4 之后被拆分成自己的 npm 包,需要单独安装 npm install body-parser

Body parser was split out into it's own npm package after v4, requires a separate install 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);

按照以下方式进行测试:

Test along the lines of:

$ curl -d '{"MyKey":"My Value"}' -H "Content-Type: application/json" http://127.0.0.1:3000/
{"MyKey":"My Value"}

这篇关于如何在 Express 应用程序中使用 JSON POST 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆