使用nodejs ftp模块上传多个文件
安装
npm i -D ftp-upload
单任务使用方法
const path = require('path')
const { FtpUpload, ftpRunSequence } = require('ftp-upload')
const ftpUpload = new FtpUpload({
remoteDomain: 'http://www.test.com',
remotePath: '/test/first',
uploadType: 'skip',
rules: [
{
test: /\.html?$/,
use: 'overwrite'
},
{
test: /\.(eot|svg|ttf|woff2?)$/,
use: 'overwrite'
}
],
threads: 3,
localPath: path.resolve(__dirname, 'dist'),
glob: '*.@(html|htm)',
host: '36.120.77.38',
port: 21,
user: 'Your user name',
password: 'Your password',
})
ftpUpload.init().on('ready', function (res) {
setTimeout(() => {
this.start(res.remoteDestPath)
}, 3000)
})
ftpUpload.on('success', function (res) {
console.log(res)
})
多任务使用方法
const ftpTasks = [
{
remoteDomain: 'http://www.test.com',
remotePath: '/test',
threads: 1,
localPath: path.resolve(__dirname, 'dist'),
glob: '*.@(html|htm)',
host: '36.120.77.38',
port: 21,
user: 'Your user name',
password: 'Your password',
},
{
remoteDomain: 'http://static.test.com',
remotePath: '/test',
uploadType: 'skip',
rules: [
{
test: /\.html?$/,
use: 'overwrite'
},
{
test: /\.(eot|svg|ttf|woff2?)$/,
use: 'overwrite'
}
],
threads: 3,
localPath: path.resolve(__dirname, 'dist/assets'),
glob: '**/*.*',
host: '36.120.77.38',
port: 21,
user: 'Your user name',
password: 'Your password',
}
]
const ftpUploadHtml = new FtpUpload(ftpTasks[0])
const ftpUploadStatic = new FtpUpload(ftpTasks[1])
ftpRunSequence([ftpUploadHtml, ftpUploadStatic], ftpUpload => {
ftpUpload.init().on('ready', res => {
setTimeout(() => {
ftpUpload.start(res.remoteDestPath)
}, 3000)
})
ftpUpload.on('success', res => {
console.log(res)
})
})