I've developed a Node.js/Express.js application that interacts with a FTP server, the problem is that if the server is offline the application crashes because I can't find a way to handle the ETIMEDOUT exception. The module I'm using for FTP is jsftp, by the way.
The part of the code where it happens looks like this:
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);
}
});
I tried appending a .on('error', function(e) { /* my code here */ }
to the router.post function, but then I get TypeError: router.post(...).on is not a function
.
Does anyone have any suggestion? I appreciate it.