0
点赞
收藏
分享

微信扫一扫

本地项目上传到云服务器 SCP2 将打包好后的项目上传到ftp服务器 js

楚木巽 2022-02-21 阅读 54

本地项目上传到云服务器 SCP2

  • 云服务器需要自己弄一台
  • 安装scp2
    npm i scp2
    
  • deploy/serve.js
     const scpClient = require('scp2');//不能用此插件上传到ftp服务器
    
     scpClient.scp('打包好之后的文件夹 ../build', {
       host: '服务器主机ip',
       username: 'root',//一般购买的服务器默认用户名为root 不是宝塔面板的用户名
       password: '服务器密码',//不是宝塔面板的密码
       path: '/www/wwwroot',//远程放文件的地址
     }, function (err) {
       if (err) {
         console.log(2222);
         throw err;
       } else {
         console.log(1111);
       }
     });
    

将打包好后的项目上传到ftp服务器 自动上传到服务器脚本 ftp (npm 包)

  • ftp服务器
  • 安装ftp
    npm i ftp
    
  • 新建一个文件夹编写自动部署脚本
  • deploy/ftp.js
    const Client = require('ftp');
    const fs = require('fs');
    const path = require('path');
    const ftp = new Client();
    
    // 本段代码地址 https://segmentfault.com/q/1010000011323948
    let walk = function (dir, done) {
      let results = [];
      fs.readdir(dir, function (err, list) {
        if (err) return done(err);
        let pending = list.length;
        if (!pending) return done(null, results);
        list.forEach(function (file) {
          file = path.resolve(dir, file);
          fs.stat(file, function (err, stat) {
            if (stat && stat.isDirectory()) {
              walk(file, function (err, res) {
                results = results.concat(res);
                if (!--pending) done(null, results);
              });
            } else {
              results.push(file);
              if (!--pending) done(null, results);
            }
          });
        });
      });
    };
    
    ftp.on('ready', function () {
    
      let tpath = path.resolve(__dirname, '../build/');
      console.log(tpath);
    
      walk(tpath, function (err, results) {
        if (err) throw err;
        results.forEach(function (filename) {
          console.log(filename);
          (function (filename) {
            //let spath = '根据filename 获取文件名';
            let spath = filename.split('build')[1];
    
            // put 是将文件内容写入到ftp远程服务器的文件中  如果远程没有文件将创建一个文件
            ftp.put(filename, spath, function (err) {
              if (err) throw err;
              console.dir("上传文件 " + spath);
              ftp.end();
            });
          })(filename)
        });
      });
    });
    
    // connect to localhost:21 as anonymous
    ftp.connect({
      host: 'ftp服务器主机地址',
      user: '用户',
      password: '密码',
    });
    

package.json 中修改

  •   "toFtp": "react-scripts build && node ./deploy/ftp.js",
      "toServe": "react-scripts build && node ./deploy/serve.js",
    

在这里插入图片描述

scp2
ftp
ssh2-sftp-client

举报

相关推荐

0 条评论