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)
 })
})
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,951评论 19 139
  • Node.js是目前非常火热的技术,但是它的诞生经历却很奇特。 众所周知,在Netscape设计出JavaScri...
    w_zhuan阅读 3,641评论 2 41
  • 入门指南 1. 简介 Quickstart会让你启动和运行一个单节点单机HBase。 2. 快速启动 – 单点HB...
    和心数据阅读 4,738评论 1 41
  • 在你无数的青丝中, 渐渐发现了银发的痕迹。 大学伊始的时候, 你假装坚强的挥手别离。 高考成绩出来的那刻, 你激动...
    雨天吖阅读 249评论 1 2
  • 2月的黎平,天空的云团是浅灰的,而天空就像是洁白的宣纸,它们组合成了一副水墨画。这种灰不是空气不好的天气里透...
    一起去探索阅读 387评论 0 1