Node JS中的分块数据

时间:2015-09-13 04:29:14

标签: node.js stream

我是节点的新手,我很难概念化你为什么会" Chunk"仅在POST请求上的数据,而不是GET请求?

if (request.method === "POST") {
  if (request.url === "/classes/messages" || request.url === "/classes/room1") {
    var statusCode = 201;
    var headers = defaultCorsHeaders;
    var body = '';
    request.on('data', function(data) {
      console.log("receiving data....", body);
      body += data;
    });
    //request ended, you can now do something with the data
    request.on('end', function() {
      var bodyObj = JSON.parse(body);
      bodyObj.objectId = Math.random() * 10;
      responseObj['results'].push(bodyObj);
      // request ended -> do something with the data. set the headers. 
      response.writeHead(statusCode, headers);
      //writing the data to json
      //response.write();
      //ending the response. 
      response.end(JSON.stringify(responseObj));
    });
    //headers['Content-Type'] = "text/plain";
  }
}

1 个答案:

答案 0 :(得分:0)

当使用POST方法用户向服务器发送数据时我们需要分析所以需要查询所有数据。 当使用GET服务器向用户发送数据时,服务器无法获取数据,不需要查询