我正在尝试从路由器引用tracks_folder
文件夹中名为public
的文件夹。
我项目的简化结构如下
app.js
routes/
tracks/
router.js
public/
tracks_folder/
基本上我想从router.js
引用tracks_folder
,以便将文件上传到该文件夹,但我无法做到。
我尝试过:
../../public/tracks_folder/
../../tracks_folder/
./tracks_folder/
但它们都不起作用。在我的app.js
我有
app.use(express.static(path.join(__dirname, 'public')));
但是例如对于第三种情况,我收到以下错误:
Error: ENOENT: no such file or directory, open 'tracks_folder/aaa.mp3'
出现错误(原生)
理论上我应该能够至少使用第一种方法引用tracks_folder
文件夹,但它确实有效。
我正在使用multer上传文件及其磁盘存储来存储它们......
以下是router.js
中简化的与问题相关的代码:
...
var multer = require('multer')
var uploadFolder = "./tracks_folder/";
function formatTrackName(originalName) {
return originalName.replace(/\s+/g, '-').toLowerCase();
}
var trackStorage = multer.diskStorage({
// used to determine within which folder the uploaded files should be stored.
destination: function(req, file, callback) {
callback(null, uploadFolder);
},
filename: function(req, res, callback) {
// req.body.name should contain the name of track
callback(null, formatTrackName(req.body.name) + ".mp3");
}
});
var upload = multer({
storage: trackStorage
})
...
...
这是我未实现的帖子方法:
router.post('/upload', upload.single("track"), function(req, res) {
});
答案 0 :(得分:0)
尝试 ./ public / tracks_folder
它对我有用