为了尝试模拟这个问题,我编写了一个基本的 Node API:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
setTimeout(() => {
res.send('Finished');
}, 10000);
});
app.listen(8282, function () {
console.log('Node API listening on port 8282...')
});
以及一个简单的 Node 客户端:
var express = require('express');
var app = express();
var axios = require('axios');
app.get('/', function (req, res) {
const config = {
url: 'http://localhost:8282',
method: 'GET',
timeout: 30000,
};
axios(config)
.then((response) => {
res.status(200).send(response.data);
})
.catch((error) => {
res.status(500).send({
code: error.code,
config: error.config,
response: error.response,
});
});
});
app.listen(8181, function () {
console.log('Node client listening on port 8181...')
});
从命令行启动两个应用程序,浏览到http://localhost:8181,然后终止 API 应用程序以模拟连接断开,客户端收到以下错误详细信息:
{
"code": "ECONNRESET",
"config": {
"transformRequest": {},
"transformResponse": {},
"timeout": 30000,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1,
"headers": {
"Accept": "application/json, text/plain, */*",
"User-Agent": "axios/0.18.0"
},
"method": "get",
"url":"http://localhost:8282"
}
}
因此,答案可能是查看错误代码 - 在本例中为 ECONNRESET。(请注意,如果请求超时,您将收到 ECONNABORTED 错误代码。)