编程语言
首页 > 编程语言> > javascript – 如何使用节点js中的scp2将文件复制到远程服务器?

javascript – 如何使用节点js中的scp2将文件复制到远程服务器?

作者:互联网

我想使用scp2包将文件从本地服务器复制到Node js中的远程服务器.首先..在复制之后使用multer上传到本地服务器的文件或将文件移动到远程服务器.

我的代码:

exports.newFileUpload =  function(req , res , next){

    var storage = multer.diskStorage({ //multers disk storage settings
      destination: function (req, file, cb) {
         cb(null, 'uploads/');
      },
      filename: function (req, file, cb) {
          var datetimestamp = Date.now();
          cb(null,  datetimestamp+ '-' +file.originalname);
      }
  });
        var upload = multer({ storage: storage, limits: { fieldSize: 25 * 1024 * 1024 }}).array('file');
        upload(req,res,function(err){
                console.log(req.body);
                console.log(req.files);
            if(err){
                res.json({error_code:1,err_desc:err});
                console.log("Error Occured", err);
                return;
            }else{


                client.scp(req.files[0].path, {
                    host: 'www.do********.in',
                    username: 'username',
                    password: '*********',
                    path: '/uploads/'
                }, function(err) {
                    console.log(req.files[0].path);
                    console.log("files uploaded in remote server");
                    res.json({error_code:0,err_desc:null});
                });
            }
        });
}

文件上传到本地服务器完全正常工作,之后到远程服务器抛出错误

错误:

{ date: 'Mon Nov 13 2017 01:00:22 GMT+0530 (India Standard Time)',
  process:
   { pid: 5664,
     uid: null,
     gid: null,
     cwd: 'D:\\sample',
     execPath: 'C:\\Program Files\\nodejs\\node.exe',
     version: 'v8.2.1',
     argv: [ 'C:\\Program Files\\nodejs\\node.exe', 'D:\\sample\\app.js' ],
     memoryUsage:
      { rss: 69619712,
        heapTotal: 45162496,
        heapUsed: 39166256,
        external: 149849 } },
  os: { loadavg: [ 0, 0, 0 ], uptime: 3537.1088452 },
  trace:
   [ { column: 11,
       file: 'util.js',
       function: 'Object.exports._errnoException',
       line: 1024,
       method: '_errnoException',
       native: false },
     { column: 20,
       file: 'util.js',
       function: 'exports._exceptionWithHostPort',
       line: 1047,
       method: '_exceptionWithHostPort',
       native: false },
     { column: 14,
       file: 'net.js',
       function: 'TCPConnectWrap.afterConnect [as oncomplete]',
       line: 1150,
       method: 'afterConnect [as oncomplete]',
       native: false } ],
  stack:[ 'Error: Can\'t set headers after they are sent.',
     '    at validateHeader (_http_outgoing.js:504:11)',
     '    at ServerResponse.setHeader (_http_outgoing.js:511:3)',
     '    at ServerResponse.header (D:\\sample\\node_modules\\express\\lib\\response.js:730:10)',
     '    at ServerResponse.send (D:\\sample\\node_modules\\express\\lib\\response.js:170:12)',
     '    at ServerResponse.json (D:\\sample\\node_modules\\express\\lib\\response.js:256:15)',
     '    at D:\\sample\\routes\\part.js:302:10',
     '    at Client.closeHandler (D:\\sample\\node_modules\\scp2\\lib\\scp.js:48:13)',
     '    at emitNone (events.js:105:13)',
     '    at Client.emit (events.js:207:7)',
     '    at Client.<anonymous> (D:\\sample\\node_modules\\scp2\\lib\\client.js:88:10)',
     '    at emitNone (events.js:105:13)',
     '    at Client.emit (events.js:207:7)',
     '    at Socket.<anonymous> (D:\\sample\\node_modules\\ssh2\\lib\\client.js:225:10)',
     '    at emitOne (events.js:115:13)',
     '    at Socket.emit (events.js:210:7)',
     '    at Pipe._handle.close [as _onclose] (net.js:549:12)' ] }

无法识别错误,等待建议或可能的方法来解决问题.

提前致谢 !

解决方法:

不能直接使用multer将文件上传到远程服务器,但我们可以使用multer-sftp,scp,ssh技术方法论

使用Node Js将文件上传到远程服务器时,我们需要注意一些事情

>用户名和密码应该是正确的
>应在远程服务器中打开相应的端口号
>远程目录应具有写访问权限

使用scp2将文件移动到远程服务器的工作代码:

exports.newFileUpload =  function(req , res , next){

    var storage = multer.diskStorage({ //multers disk storage settings
      destination: function (req, file, cb) {
         cb(null, 'uploads/');
      },
      filename: function (req, file, cb) {
          var datetimestamp = Date.now();
          cb(null,  datetimestamp+ '-' +file.originalname);
      }
  });
        var upload = multer({ storage: storage, limits: { fieldSize: 25 * 1024 * 1024 }}).array('file');
        upload(req,res,function(err){
                console.log(req.body);
                console.log(req.files);
            if(err){
                res.json({error_code:1,err_desc:err});
                console.log("Error Occured", err);
                return;
            }else{


                client.scp(req.files[0].path, {
                    host: 'host',
                    username: 'username',
                    password: '*********',
                    path: '/uploads/'
                }, function(err) {
                    console.log(req.files[0].path);
                    console.log("files uploaded in remote server");
                });
            }
        });
}

注意:应该安装所需的包以及在代码中声明需要的东西.

参考文献:multer-sftp,scp2,node-ssh

标签:multer,javascript,file-upload,node-js,scp
来源: https://codeday.me/bug/20190828/1749668.html