如何在Express应用程序中使用JSON

如何在Express应用程序中使用JSON

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

问题描述

 
{
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个响应,但是其他两种方法从不跑。为什么?

解决方案

我认为你正在使用响应对象与请求的对象



响应对象用于将HTTP响应发送回主叫客户端,而您要访问请求的正文。请参阅,其中提供了一些指导。



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

  var express = require('express')
,app = express.createServer();

app.use(express.bodyParser());

app.post('/',function(request,response){
console.log(request.body); //你的JSON
response.send(request)身体); //回显结果
});

app.listen(3000);

沿着以下行进行测试:

  $ curl -d'{MyKey:My Value}'-HContent-Type:application / jsonhttp://127.0.0.1:3000/ 
{MyKey:我的价值}

更新为Express 4 +



体验解析器在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); //你的JSON
response.send(request)身体); //回显结果
});

app.listen(3000);


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

(
        {
        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. Why is that?

解决方案

I think you're conflating the use of the response object with that of the 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.

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.

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+

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应用程序中使用JSON POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 03:00