Node 使用 SFTP 上传文件到服务器

安装模块node-ssh

npm i node-ssh
Example
var path, node_ssh, ssh, fs

fs = require('fs')
path = require('path')
node_ssh = require('node-ssh')
ssh = new node_ssh()
const password = ''

//connect sftp
ssh.connect({
 host: '',
 username: '',
 port: 22,
 password,
 tryKeyboard: true,
 onKeyboardInteractive: (name, instructions, instructionsLang, prompts, finish) => {
     if (prompts.length > 0 && prompts[0].prompt.toLowerCase().includes('password')) {
       finish([password])
     }
   }
})

//upload file
.then(function() {
 ssh.putFile('localFile path', 'remotFile path').then(function() {
   console.log("The File thing is done")
 }, function(error) {
   console.log("Something's wrong")
   console.log(error)
 })
})

你可能感兴趣的:(Node 使用 SFTP 上传文件到服务器)