我正在尝试使用Node JS内置的
这是我一直在使用的代码:
以下是预期输出(来自 cURL ):
http
和https
模块的request
函数(我知道有第三方包装器,但我想自己解决问题)。 我遇到的问题是响应中的缓冲数据在末尾部分被截断。 在使用cURL
测试时不会出现此问题。这是我一直在使用的代码:
const { request: httpRequest } = require("http");
const { request: httpsRequest } = require("https");
const parseURLData = (url, init) => {
const { hostname, protocol, port: somePort, pathname, search } = new URL(url);
const port = +somePort || (protocol === "https:" ? 443 : 80);
const options = { hostname, port, path: pathname + search, ...init };
return [protocol, options];
};
const makeRequest = (url, init = { method: "GET" }) => {
const [protocol, options] = argumentsToOptions(url, init);
const request = protocol === "http:" ? httpRequest : httpsRequest;
return new Promise((resolve, reject) =>
request(options, (res) => {
res.on("error", reject);
resolve(res);
})
.on("error", reject)
.end()
);
};
// not using `async/await` while testing
makeRequest("https://jsonplaceholder.typicode.com/users/1/")
.then((res) =>
new Promise((resolve) =>
res.on("data", (buffer) => {
resolve(buffer.toString("utf8")); // part of data is cut off
// resolve(JSON.parse(buffer.toString()));
})
)
)
.then(console.log)
.catch(console.error);
以下是预期输出(来自 cURL ):
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
}
以下是实际输出,不知何故每次运行代码结果略有不同:
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"p
这个问题的正确解决方案是什么?我应该怎样获取请求中的所有数据?