【发布时间】: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')
});
【问题讨论】:
-
我不确定您是否研究过 axios 文档,请查看此github.com/axios/axios 您可以使用自定义配置创建新的 axios 实例。 axios.create([config]) const instance = axios.create({ baseURL: 'some-domain.com/api', timeout: 1000, headers: {'X-Custom-Header': 'foobar'} });
-
我很确定服务器正在关闭连接。我会在这里看看nodejs.org/dist/latest-v8.x/docs/api/…。默认值设置为 2 分钟。
标签: node.js reactjs express axios