我正在使用 nodejs 并向服务器发送 http post 请求。在服务器端我正在运行 php。我的服务器返回大约 9KB 的正确数据,但 Nodejs 客户端中的数据已终止。如果数据小于 6KB,则可以正常工作。以下是我的代码
var reqPost = https.request(optionspost, function(res) {
res.on('data', function(d) {
Console.log('Cloud Resp:', d);
var jsonObj = JSON.parse(d);
});
});
Run Code Online (Sandbox Code Playgroud)
我的 print Console.log('Cloud Resp:', d) 仅打印最多 8KB 的数据。有人可以帮助我了解这个限制是由nodejs还是其他东西强加的,以及如何增加限制
我认为您的数据在传输PHP 服务器 ---> 节点服务器期间被分块
https模块从节点端请求(如果我错了,请纠正我)因此,data如果您需要连接块。但你只能在事件中解析它end。如果您在data事件中进行解析,则会JSON.parse()因数据不完整而显示错误
这是示例代码,经我测试,它可以处理 500kb 的数据。基本上原生节点在代码级别没有数据限制。
var http = require("https");
var options = {
"method": "GET",
"hostname": "c16db448-d912-4ce8-823a-db6c51e09878.mock.pstmn.io"
};
var req = http.request(options, function (res) {
var chunks = '';
res.on("data", function (chunk) {
console.log(chunk.length)
chunks += chunk;
});
res.on("end", function () {
const object = JSON.parse(chunks)
console.log(object.length)
console.log(Buffer.byteLength(chunks, 'utf8') / 1024 + " kbytes");
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3864 次 |
| 最近记录: |