我有一个简单的Web应用程序,它登录到面向客户的Web服务器并在get请求中下载日志文件。当您将get请求发送到某个路由时,模块会启动通过sftp连接到Web服务器并在本地下载日志,以用于2个单独的服务器。出于某种原因,我的链中的last then()方法在promise解析之前被触发。
router.js:
var express = require('express');
var router = express.Router();
var gather = require('../api/gather');
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('home');
});
router.get('/api/gather',function(req,res,next){
console.log('sending web9 target');
gather.logs(['WEB9'])
.then((logsFound)=>{
console.log('sending web 11 target');
gather.logs(['WEB11',logsFound])
})
.then((logsFound)=>{
console.log(logsFound);
console.log('downloading finished rendering home with message');
res.render('home',{message: logsFound+' Log Files Ready for Pickup'});
})
.catch((e)=>{
res.render('home',{message: e});
});
});
module.exports = router;
/api/gather.js
//require ftp modules and define client
let Client = require('ssh2-sftp-client');
let sftp = new Client();
var fs = require('fs');
let sftp2 = new Client();
//set server configs
var boomeConfig = {
host: process.env.FTP_HOST,
port: process.env.FTP_PORT,
username: process.env.FTP_USER,
password: process.env.FTP_PASS
};
var webServerConfig = {
port: process.env.WEB_PORT,
username: process.env.WEB_USER,
password: process.env.WEB_PASS
};
let message;
const logDir = '/opt/foobar/logs/';
module.exports.logs = function(req, res){
console.log('hit gather module');
const target = req[0];
let logsFound = req[1] || 0;
return new Promise((resolve,reject)=>{
if(target === 'WEB9'){
webServerConfig.host = process.env.WEB9_HOST;
}else if(target === 'WEB11'){
webServerConfig.host = process.env.WEB11_HOST;
}else{
message = 'No Server Target Found';
resolve(message);
}
sftp.connect(webServerConfig)
.then(()=>{
return sftp.list(logDir);
})
.then((data)=>{
for(var i = 0; i < data.length; i++){
const remoteFileName = logDir + data[i].name;
const localFileName = './downloads/'+target+'/' + data[i].name;
//console.log('saving file '+localFileName);
sftp.get(remoteFileName).then((stream) => {
stream.pipe(fs.createWriteStream(localFileName));
});
}
console.log('Found '+data.length+' Logs on '+target);
sftp.end();
logsFound += data.length;
console.log('Got '+target+' Files Moving to next');
if(target ==='WEB11'){
console.log('Total count '+logsFound);
}
}).then((logsFound)=>{
resolve(logsFound);
})
.catch((e)=>{
console.log('error: '+e);
reject(e);
});
})
}
我的主路线正在使用logsFound渲染为未定义。
我唯一能想到的是它可能的
sftp.get(remoteFileName).then((stream) => {
stream.pipe(fs.createWriteStream(localFileName));
});
当promise结算时,仍然是流文件。
答案 0 :(得分:0)
问题是我正在解决我的承诺,但是我的路线编写方式我正在创建Promise构造函数反模式。感谢Bergi和Kevin B.
编辑路线
var express = require('express');
var router = express.Router();
var gather = require('../api/gather');
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('home');
});
router.get('/api/gather',function(req,res,next){
console.log('sending web9 target');
return gather.logs(['WEB9'])
.then((logsFound)=>{
console.log('sending web 11 target');
return gather.logs(['WEB11',logsFound])
})
.then((logsFound)=>{
console.log(logsFound);
console.log('downloading finished rendering home with message');
res.render('home',{message: logsFound+' Log Files Ready for Pickup'});
})
.catch((e)=>{
res.render('home',{message: e});
});
});
module.exports = router;