Kuan
Kuan

Reputation: 11389

Decide the upload folder location of Multer middleware

All:

I am new to Express.js, when I deal with file uploading with Multer (https://github.com/expressjs/multer), I specify a multer middleware object:

var upload = multer({dest: 'uploads/'});

But one thing confuse me so much is: No matter where I put this line code( either in sub router file, or app.js), it always creates "uploads" folder under project root( same folder with app.js, views, routes, public... located), even I change it to "./uploads/", it still creates uploads folder under project root.

This confuses me so much, cos when I compare this with require() function, it seems require() uses relative path based on where it is get called while multer is not? I wonder if my understand is correct? And how to change it to relative if mine is correct?

Thanks

Upvotes: 2

Views: 4257

Answers (2)

gmaghera
gmaghera

Reputation: 545

It seems to be based on the value of path.resolve(). Where path is defined as var path = require('path').

Upvotes: 2

mscdex
mscdex

Reputation: 106696

You can always specify an absolute path (that is relative to the current module) to remove any doubt:

var upload = multer({dest: __dirname + '/uploads/'});

Upvotes: 2

Related Questions