gpt4 book ai didi

node.js - 如何在nodejs中保持持久的ftp连接

转载 作者:太空宇宙 更新时间:2023-11-04 02:40:00 25 4
gpt4 key购买 nike

你能帮我制作一个连接持久脚本吗?我使用jsftp Node 模块连接到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;

最佳答案

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

// 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') );
}

}
}

关于node.js - 如何在nodejs中保持持久的ftp连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17484583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com