我在通过运行在Google应用引擎中的节点api从ftp服务器下载ftp数据(在这种情况下为pdf)时遇到问题。到目前为止,这是我的代码:
const Client = require('node-ftp')
const streams = require('memory-streams')
exports.downloadRekla = async function (req, res, next) {
let writer = new streams.WritableStream();
let c = new Client();
let config = {
host: "xxx",
user: "xxx",
password: "xxx",
secure: false
}
c.on('ready', function () {
c.get(`${req.params.banr}_Reklamation.pdf`, function (err, stream) {
if (err) {
// FTP Code 550 = Requested action not taken. File unavailable (e.g., file not found, no access).
if (err.code == 550) {
// Don't send if no file with this name exists
console.log(`${req.params.banr}_Reklamation.pdf nicht vorhanden.`);
res.status(200).send();
c.end();
} else {
throw err;
}
} else {
stream.once('close', function () {
writer.end();
console.log('send res');
res.send(writer.toBuffer());
c.end();
console.log('bye');
});
console.log('write data in writable stream');
stream.pipe(writer);
console.log('done writing stuff in stream')
}
});
});
c.on('error', function () {
console.log('close connection');
c.end();
setTimeout(() => {
console.log('attempting to reconnect');
c.connect(config);
console.log('reconnected');
}, 2000);
});
console.log('attempting to connect to ftp server');
c.connect(config);
console.log('connected');}
问题是,我没有得到任何回应,只有502错误的网关...我在这里做错的任何见解?
此外,计划是将pdf数据写入可写流中,将其转换为缓冲区,然后在前端使用该数据制成pdf。