【发布时间】: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.js 的request 模块并设置5ms 的超时。但是当我运行它时,它需要 2 分钟才能发回超时错误响应。
这里我需要如果此请求无法在5 milisecond 内发回响应,那么它应该返回超时错误。
【问题讨论】: