我正在尝试将文件上传到FTP。我正在使用Express.js,Node,并且在此博客上找到了一个非常不错的教程:
https://attacomsian.com/blog/uploading-files-nodejs-express
那么,一切看起来都很好,但是我可以在哪里将配置数据传输到FTP(用户,主机,密码...)?
据我了解,express-fileupload是一种中间件,因此,我需要执行一个额外的步骤吗?还是我必须将凭据传递到FTP的地方?
接下来,博客示例中的代码:
const express = require('express');
const fileUpload = require('express-fileupload');
const cors = require('cors');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const _ = require('lodash');
const app = express();
// enable files upload
app.use(fileUpload({
createParentPath: true
}));
//add other middleware
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(morgan('dev'));
app.post('/upload-avatar', async (req, res) => {
try {
if(!req.files) {
res.send({
status: false,
message: 'No file uploaded'
});
} else {
//Use the name of the input field (i.e. "avatar") to retrieve the uploaded file
let avatar = req.files.avatar;
//Use the mv() method to place the file in upload directory (i.e. "uploads")
avatar.mv('./uploads/' + avatar.name);
//send response
res.send({
status: true,
message: 'File is uploaded',
data: {
name: avatar.name,
mimetype: avatar.mimetype,
size: avatar.size
}
});
}
} catch (err) {
res.status(500).send(err);
}
});
//start app
const port = process.env.PORT || 3000;
app.listen(port, () =>
console.log(`App is listening on port ${port}.`)
);
希望您能帮助我。也许很容易,但是在这个时候,我的大脑不再工作了。
预先感谢