1

你能帮我制作一个连接持久脚本吗?我使用 jsftp 节点模块连接到 ftp 服务器。我需要做的是检查用户每次发送请求时是否已经通过身份验证。提前致谢!这是我的代码:

var Ftp = require('jsftp');

var dumpLog =  function (event){
        console.log('Code: '+ event.code);
        console.log('Message: '+ event.text);
}

var FtpController = {

    index : function (req , res) {
        res.view('ftp/login');
    },

    auth : function (req , res){

        // Initialize some common variables
        var user = req.param('user');
        var pass = req.param('pass');

        var ftp = new Ftp({
            host: req.param('host'),
            port: req.param('port') // Defaults to 21
        });

        ftp.auth( user, pass, function (err , auth_res){
            if (err) throw err;

            dumpLog(auth_res);
        });

        res.view('ftp/folder');
    },

    serve_folder : function(req,res){
        res.view('ftp/folder');
    },

};
module.exports = FtpController;
4

1 回答 1

0

执行此类操作的最佳方法可能是一项策略,因为您希望能够在构建应用程序时将检查应用于各种控制器。您的政策可能如下所示:

// policies/ftpAuthenticated.js
module.exports = function loginToFTP (req, res, next) {
  if (req.session.ftpAuthenticated) {
    // Onward!
    next();
  }
  else {
    // authenticate here (we assume it works in this example)
    var success = true;
    if (success) {
      // Track that the user is connected via ftp for next time
      req.session.ftpAuthenticated = true;
      // save the connection object
      req.session.ftp = theFTPConnectionThing;
      next();
    }

    // if an error occurs, use the default error handler
    else {
      next( new Error('Sorry, an error occurred authenticating with FTP') );
    }

  }
}
于 2013-08-17T19:31:18.130 回答