gpt4 book ai didi

node.js - Node JS 中的数据分块

转载 作者:太空宇宙 更新时间:2023-11-04 02:22:24 24 4
gpt4 key购买 nike

我是 Node 新手,无法理解为什么只在 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

关于node.js - Node JS 中的数据分块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32546306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com