【问题标题】:How can I stop axios from retrying the request after 2 mins如何阻止 axios 在 2 分钟后重试请求
【发布时间】:2018-05-30 15:53:08
【问题描述】:

我已经开发了一个 nodejs 服务器和响应客户端应用程序。客户端使用 axios 向服务器请求信息。此信息必须从数据库中检索,大约需要 5 分钟。但是客户端会在 2 分钟后重试请求。如何让客户等待 5 分钟。

客户端代码

  axios.get(apiServer + 'teradata/'+table).then( (resp) => {
    console.log(resp.data.data)
    )

在服务器中,我在下面尝试过。但没有任何效果。

server.on('connection', function(socket) {
    //Teradata takes too long to respond. Set timeoute to 3 mins
    socket.setTimeout(0);
  })

server.on('connection', function(socket) {
    //Teradata takes too long to respond. Set timeoute to 3 mins
    socket.setTimeout(60 * 6 * 1000); 
    // 30 second timeout. Change this as you see fit.
  })

更新: 在进一步调试中,我看到 1.重试源自浏览器。因为,重试发生在 Firefox 中,但不在 chrome 中。 2. 连接正好在 2 分钟后断开。这会导致 Firefox 重试。

我在 express js 中使用了以下代码,发现它确实断开了连接。但我不知道是谁断开了它。是axios还是express js?

req.on("close", function() {
    console.log('request closed unexpectedly')
  });

  req.on("end", function() {
    console.log('equest ended normally')
  });    

【问题讨论】:

标签: node.js reactjs express axios


【解决方案1】:

根据文档,您也可以创建 axios 实例并配置超时。

const instance = axios.create({
  baseURL: 'https://some-domain.com/api/',
  timeout: 5000,
  headers: {'X-Custom-Header': 'foobar'}
});

【讨论】:

  • 我试过了,还是有同样的问题。我注意到一件事,即使在添加超时之后,标题也没有变化。不确定是否必须添加超时标头
猜你喜欢
  • 1970-01-01
  • 2017-05-31
  • 1970-01-01
  • 2021-07-09
  • 1970-01-01
  • 1970-01-01
  • 2021-06-01
  • 2018-06-28
  • 2022-01-28
相关资源
最近更新 更多