可以使用WinSCP客户端测试是否连通服务器
一,服务器是Linux
1, 安装scp2
npm install scp2 --save-dev
2, 根目录下创建deploy.js
const client = require("scp2");
const ora = require("ora");
const chalk = require("chalk");
const spinner = ora(chalk.green("正在发布。。。"));
spinner.start();
/*
host: 服务器ip
port:scp上传的端口号 (默认:22)
username:服务器账号
password:服务器密码
path:部署到服务器的路径
*/
const server = {
host: "11.11.11.11",
port: 22,
username: "root",
password: "******",
path: "/html"
};
client.scp(
"./dist/",
{
host: server.host,
port: server.port,
username: server.username,
password: server.password,
path: server.path
},
err => {
spinner.stop();
if (!err) {
console.log(chalk.green("成功"));
} else {
console.log(err);
}
}
);
3,运行
node deploy.js
二,服务器是windows
注:需要安装插件OpenSSH,请参考WinSCP之Windows与Windows之间传输文件
1,安装 ssh2-sftp-client
npm install ssh2-sftp-client
2, 根目录下创建deploy.js
const ssh2SftpClient = require("ssh2-sftp-client");
const ora = require("ora");
const chalk = require("chalk");
const spinner = ora(chalk.green("正在发布。。。"));
const path = require("path");
spinner.start();
/*
localPath: 本地需要上传的文件,注意不是文件夹
remotePath:服务器的路径并设置上传后的名称
*/
const localPath = path.join(__dirname, "/dist/dist.zip");
const remotePath = "C:/Users/Administrator/Desktop/部署测试/rename.zip";
/*
host: 服务器ip
port:scp上传的端口号 (默认:22)
username:服务器账号
password:服务器密码
*/
const server = {
host: "192.168.1.89",
port: 22,
username: "administrator",
password: "123"
};
function putFile(localPath, remotePath) {
const sftpClient = new ssh2SftpClient();
sftpClient
.connect({
host: server.host,
port: server.port,
username: server.username,
password: server.password
})
.then(() => {
return sftpClient.fastPut(localPath, remotePath, server);
})
.then(() => {
spinner.stop();
console.log(chalk.green("成功"));
})
.catch(err => {
spinner.stop();
console.log(err, "catch error");
});
}
putFile(localPath, remotePath);
3,运行
node deploy.js
注:临时起意,写的比较简单,后续继续更新!