如何在Node.js中不接收响应的情况下检测内容长度(响应大小)?

时间:2019-02-28 13:10:25

标签: javascript node.js request content-length

我正在通过使用nodejs请求模块从nodejs请求一个休息服务器。是否可以确定将要返回给我的数据的大小(在接受数据之前)?返回给我的数据超出了我确定的数量。这里的目的是确保我的网络未锁定。

我的请求部分如下;

const request = registry.get("requestPromiseNative");
const logger = registry.get("logger");
let env = registry.get("env");

 class SmartContractFetch {
constructor() {
this.dataSizeFetched = 0; //in bytes
}

async fetchData(method, url, data = {}, header = {}) {
try {
  logger.info("Smart contract fetch işlemi başladı.");
  if (method.toUpperCase() === "POST") {
    console.log("POS içinde  : " + JSON.stringify(data));
    let response = await request.post({
      url: url,
      resolveWithFullResponse: true,
      form: data,
      headers: header
    });
    let responseJSON = JSON.parse(response.body);
    this.dataSizeFetched += response.body.length;
    logger.info(
      "Smart contract fetch işlemi başarıyla tamamlandı. Gelen Data : " +
        responseJSON
    );
    return responseJSON;
  } else if (method.toUpperCase() === "GET") {
    console.log("GET içinde  : " + JSON.stringify(data));
    let response = await request.get({
      url: url,
      resolveWithFullResponse: true,
      form: data,
      headers: header
    });
    this.dataSizeFetched += response.body.length;
    let responseJSON = JSON.parse(response.body);
    logger.info(
      "Smart contract fetch işlemi başarıyla tamamlandı. Gelen Data : " +
        responseJSON
    );
    return responseJSON;
  }
} catch (error) {
  logger.error(
    "Smart contract fetch işlemi başarısız. Sonuç : " + error.message
  );
  throw new Error(error);
}
 }
 }
 module.exports = SmartContractFetch;

0 个答案:

没有答案