我开发了一个与 FTP 服务器交互的 Node.js/Express.js 应用程序,问题是如果服务器离线,应用程序就会崩溃,因为我找不到处理 ETIMEDOUT 异常的方法。顺便说一下,我用于 FTP 的模块是 jsftp。
发生这种情况的代码部分如下所示:
router.post("/", upload, function(req, res, next) {
try {
ftp.auth(ftp.user, ftp.pass, function(error) {
if(error) {
res.status("500");
res.end("Error: Couldn't authenticate into the FTP server.");
console.log(error);
} else { // Authenticated successfully into the FTP server
/* some code here */
}
});
} catch(error) { // Probably timeout error
res.status("500");
res.end("Internal Server Error");
console.log(error);
}
});
我尝试将 .on('error', function(e) {/* my code here */}
附加到 router.post 函数,但随后我得到 TypeError: router.post(...).on 不是函数
。
有人有什么建议吗?我很感激。
最佳答案
您的问题类似于 this question
这些步骤是处理错误并重试。
http.Client.on('error', function (err) {
/* handle errors here */
if (err.message.code === 'ETIMEDOUT') {
/* apply logic to retry */ }
})
使用node-retry以保持重试逻辑简单。
关于javascript - 如何在 Express.js 中处理 ETIMEDOUT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47877747/