【发布时间】:2020-10-11 11:07:44
【问题描述】:
我正在使用此函数调用外部 API
const fetch = require('node-fetch');
fetchdata= async function (result = {}) {
var start_time = new Date().getTime();
let response = await fetch('{API endpoint}', {
method: 'post',
body: JSON.stringify(result),
headers: { 'Content-Type': 'application/json' },
keepalive: true
});
console.log(response)
var time = { 'Respone time': + (new Date().getTime() - start_time) + 'ms' };
console.log(time)
return [response.json(), time];
}
问题是我不确定每次我使用这个函数时 node.js 是否重用了到 API 的 TCP 连接,即使我定义了 keepalive 属性。
重用 TCP 连接可以显着提高响应时间
欢迎提出任何建议。
【问题讨论】:
标签: node.js http fetch node-fetch