【问题标题】:Unable to get request timeout response using node.js无法使用 node.js 获取请求超时响应
【发布时间】:2021-05-13 23:10:46
【问题描述】:

我正在尝试在 node.js 的 request 模块内设置超时值,但根据给定的时间,它不会返回响应。我在下面解释我的代码。

postRequestOptions.url = `${nsoObj.protocol}://${nsoObj.ipAddress}:${nsoObj.httpPortNbr}/jsonrpc`;
    postRequestOptions.headers = {
      'Content-Type': 'application/json'
    };
    postRequestOptions.body = {
      jsonrpc: '2.0',
      id: 1,
      method: 'login',
      timeout: 5000,
      params: {
        user: nsoObj.userName,
        passwd: nsoObj.password
      }
    };

    request(postRequestOptions, (error, response, loginResponse) => {
      console.log('\n error: ', error);
      console.log('\n loginResponse: ', loginResponse);

      if (error || !loginResponse.id) {
        responseObj = {
          status : 'error',
          msg : `Error occurred while performing Login into "${nsoObj.nsoNickName}"  Instance. ${error}`,
          body : null
        };
        reject(responseObj);
      } else {
        loginResponse.sessionId = response.headers['set-cookie'][0];
        responseObj = {
          status : 'success',
          msg : `Successfully performing Login into "${nsoObj.nsoNickName}"  Instance`,
          body : loginResponse
        };
        resolve(responseObj);
      }
    });
  });

这里我使用node.jsrequest 模块并设置5ms 的超时。但是当我运行它时,它需要 2 分钟才能发回超时错误响应。

这里我需要如果此请求无法在5 milisecond 内发回响应,那么它应该返回超时错误。

【问题讨论】:

    标签: node.js request timeout


    【解决方案1】:

    从正文中删除超时并使用postRequestOptions.timeout = 5000; 这样做:

    postRequestOptions.url = `${nsoObj.protocol}://${nsoObj.ipAddress}:${nsoObj.httpPortNbr}/jsonrpc`;
        postRequestOptions.headers = {
          'Content-Type': 'application/json'
        };
        postRequestOptions.body = {
          jsonrpc: '2.0',
          id: 1,
          method: 'login',
          params: {
            user: nsoObj.userName,
            passwd: nsoObj.password
          }
        };
        postRequestOptions.timeout = 5000;//should be outside the body
        request(postRequestOptions, (error, response, loginResponse) => {
          console.log('\n error: ', error);
          console.log('\n loginResponse: ', loginResponse);
    
          if (error || !loginResponse.id) {
            responseObj = {
              status : 'error',
              msg : `Error occurred while performing Login into "${nsoObj.nsoNickName}"  Instance. ${error}`,
              body : null
            };
            reject(responseObj);
          } else {
            loginResponse.sessionId = response.headers['set-cookie'][0];
            responseObj = {
              status : 'success',
              msg : `Successfully performing Login into "${nsoObj.nsoNickName}"  Instance`,
              body : loginResponse
            };
            resolve(responseObj);
          }
        });
      });
    

    【讨论】:

    • 我只是按照您的要求添加了相同的问题。大约需要 2 分钟。
    • 我想,你的请求完全正确,为什么方法是登录?
    • 不,它在阳性测试用例中给出正确的结果。这是 json rpc 调用。
    • 能把npm模块的链接发给我吗?
    • 它与 npm 中的请求模块相同。现在我正在尝试connect-timeout 模块。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    • 2022-01-12
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 2021-02-07
    相关资源
    最近更新 更多