在我的node.js应用程序中,我使用ssh 2-sftp-client每5秒上传一张图片。
上传工作正常,但在重复该功能时,我收到错误消息
node .\upload.js
uploaded screenshot to server as test_screenshot_uploadI3Mjc.png (first run)
Error: connect: An existing SFTP connection is already defined (second run)
我的代码是:
let Client = require('ssh2-sftp-client');
let sftp = new Client();
function uploadScreenshot() {
randomNumber = Buffer.from(Math.random().toString()).toString("base64").substring(10,15);
filename = "test_screenshot_upload"+randomNumber+".png";
sftp.connect({
host: 'ssh.strato.de',
port: '22',
username: 'user',
password: 'pass'
}).then(() => {
sftp.put('screenshot.png', '/path/folder/folder/'+filename);
}).then(data => {
console.log("uploaded screenshot to server as " + filename);
}).catch(err => {
console.log(err, 'catch error');
});
}
function foo() {
uploadScreenshot();
setInterval(foo, 5000);
}
foo();
我试着在sftp.put后添加sftp.end();
,但是没有用。你能给予我一个提示吗?我做错了什么?
1条答案
按热度按时间xriantvc1#
我所需要做的就是
进入
它现在起作用了。