node.js - NodeJS - 使用 "Sequest"检查 SFTP 远程文件是否存在

标签 node.js ssh sftp remote-access

我是 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/

相关文章:

node.js - Nginx proxy_pass 到带有尾部斜杠的变量失败

javascript - 是否可以从基类[node JS]中的静态方法实例化子类?

python - vscode 扩展的 package.json 文件中的奇怪语法

javascript - node.js - 使用 Twilio 拨号

git - 如何创建依赖于托管在 gitlab 子组中的私有(private) go 包的 docker 镜像?

java - 使用公钥的 sftp 无法正常工作

git clone 无法访问 docker 容器内的主机

github - SSH 访问 GitHub 存储库

java - channel Sftp : create new or temporary file

python - 如何在 Linux 中获取包含感兴趣的特定文件的最新文件夹并在 Python 中使用 Paramiko 下载该文件?