我使用节点 jsftp 模块和sailsjs。我的 ftp 会话有问题,我认为这就是问题所在。如我错了请纠正我。我只需要在用户登录时显示文件列表。
这就是问题所在。登录后,用户被重定向到主页。但是,重定向后ftp实例消失了。每次重定向时都需要重新连接“ftp.auth”吗?
请看下面的代码。提前致谢!
/*---------------------
:: Ftp
-> controller
---------------------*/
var Ftp = require('jsftp');
var dumpLog = function (event){
console.log('Code: '+ event.code);
console.log('Message: '+ event.text);
}
var ftp;
var FtpConnect = function(){
console.log('ftpconnect');
ftp = new Ftp({
host: req.session.user,
port: req.session.user // Defaults to 21
});
ftp.auth( req.session.user , req.session.user , function (err , auth_res){
console.log('reconnecting....');
if(err){
dumpLog(err);
}else{
dumpLog(auth_res);
}
});
}
var FtpController = {
index : function (req , res) {
if(req.session.user == undefined )
{
res.view();
}else{
res.redirect('/serve_folder');
}
},
login : function (req,res){
res.view('ftp/login');
},
auth : function (req , res){
console.log('this is /auth');
var ftp = new Ftp({
host: req.param('host'),
port: req.param('port') // Defaults to 21
});
ftp.auth( req.param('user') , req.param('pass') , function (err , auth_res){
if(err){
dumpLog(err);
res.redirect('/');
}else{
dumpLog(auth_res);
if (auth_res.code == 230)
{
console.log('store session');
//store credentials to session for persistent connection
req.session.user = req.param('user');
req.session.pass = req.param('pass');
req.session.host = req.param('host');
req.session.port = req.param('port');
console.log('redirecting: /serve_folder');
ftp.ls('/',function (err,files){
console.log(files);
});
res.redirect('/serve_folder');
}
}
});
},
serve_folder : function(req,res){
console.log('this is /serve_folder');
ftp.ls('/',function (err,files){
console.log(files);
});
res.view('ftp/folder');
},
};
module.exports = FtpController;