1

I am trying to refer to a folder called tracks_folder within my public folder from a router.

The simplified structure of my project is the following

app.js
routes/ 
      tracks/
            router.js
public/
      tracks_folder/

Basically I want to refer from router.js to tracks_folder, in order to upload a file to that folder, but I am not managing to do it.

I have tried with:

  1. ../../public/tracks_folder/
  2. ../../tracks_folder/
  3. ./tracks_folder/

But none of them work. In my app.js I have

app.use(express.static(path.join(__dirname, 'public')));

but for example for the third case I receive the following error:

Error: ENOENT: no such file or directory, open 'tracks_folder/aaa.mp3'

at Error (native)

Theoretically I should be able to refer to the tracks_folder folder using at least the first method, but it does work.

I am using multer to upload files and its disk storage to store them...

The following is the simplified problem-related code inside 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
})

...
...

And this is my unimplemented post method:

router.post('/upload', upload.single("track"), function(req, res) {

});
nbro
  • 15,395
  • 32
  • 113
  • 196
  • You should probably include the code where you are trying to write the file. – mscdex Dec 04 '15 at 00:10
  • @mscdex I edited my question with the requested details. By the way, this disk storage mechanism really handles the whole process of storing the file, or just stores the files temporarily (and I should then handle the write of the file using e.g. `fs`'s functions)? Honestly it is not clear at all from the documentation... – nbro Dec 04 '15 at 00:16

1 Answers1

0

try ./public/tracks_folder
It worked for me

Gaurav Gupta
  • 1,929
  • 4
  • 21
  • 40