在Node JS中分块数据



我是新的节点,我有麻烦概念化为什么你会"块"数据上的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";
  }
}

当使用POST方法用户发送数据到服务器,我们需要分析,所以需要丢弃所有的数据。当使用GET服务器向用户发送数据时,服务器不需要获取数据,不需要chuck

相关内容

  • 没有找到相关文章

最新更新