Home > other >  Cannot resolve nodejs files path on exported to another directory
Cannot resolve nodejs files path on exported to another directory

Time:10-18

I have a problem with display file path because not sure why but node is unable to display it.

If I am using req.file.filename it is working just fine but I need to process the loaded file so when I am exporting to a directory I need to use something like the second variable FILE_OUTPUT and in that case the path is not recognised anymore, any idee why is behaving like that?

The path is looking like: /Users/jimmy/work/task/running/public/output/xs_1634558545319.png and should be : output/xs_1634558545319.png

app.post("/exit", upload.single("pic"), function (req, res, next) {

  const FILE_INPUT = path.join(__dirname, req.file.path) ;

  const FILE_OUTPUT = path.join(__dirname, `/public/output/xs_${req.file.filename}`); ;
  
  im.convert([FILE_INPUT, '-resize', '100x100', FILE_OUTPUT], 
  function(err, stdout){
    if (err) throw err;

    console.log('stdout:', stdout, FILE_OUTPUT, FILE_INPUT);

  });
});

CodePudding user response:

you need to use path.basename:

const FILE_OUTPUT = path.basename(`/public/output/xs_${req.file.filename}`);
  • Related