我是 NodeJS 新手,我正在使用“Sequest”包来读取 SFTP 远程文件的内容。效果很好。但是,如果我尝试读取的文件不存在,则会抛出异常并且应用程序不会进一步响应。
所以我想在尝试读取文件之前检查该文件是否存在。由于我使用的是库函数(sequest.get),因此由于缺少指定的文件,我无法处理库方法中发生的异常。
下面是我的代码:
var reader = sequest.get('xyz@abc', fileName, opts);
reader.setEncoding('utf8');
reader.on('data', function(chunk) {
return res.send(chunk);
});
reader.on('end', function() {
console.log('there will be no more data.');
});
引用号:https://github.com/mikeal/sequest#gethost-path-opts
Sequest ( https://github.com/mikeal/sequest ) 是 SSH2 - ( https://github.com/mscdex/ssh2 ) 的包装器。
非常感谢任何帮助。谢谢。
最佳答案
您可以监听error
事件来处理此类情况。
var reader = sequest.get('xyz@abc', fileName, opts);
reader.setEncoding('utf8');
reader.on('data', function(chunk) {
return res.send(chunk);
});
reader.on('end', function() {
console.log('there will be no more data.');
});
reader.on('error', function() {
console.log('file not found or some other error');
});
关于node.js - NodeJS - 使用 "Sequest"检查 SFTP 远程文件是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33248656/