Alon
Alon

Reputation: 2929

Express static server - set the dir folder

I create simple express static server.

app.get("/media/:filename/" ,express.static(__dirname + /media/));

my problem is that if I want to get a file from the server the file need to be under the media/media folder.

I know that I can write

app.get("/media/:filename/" ,express.static(__dirname));

and it would work, but I look for another solution that I dont have to create the root folder to be the static server root.

I though to use redirect to another path but I think this is not good solution..

Upvotes: 4

Views: 672

Answers (1)

Krzysztof Sztompka
Krzysztof Sztompka

Reputation: 7204

If you write :

app.get("/media" ,express.static(__dirname + /media/));

then server will serve all files from root media directory.

if you write url:

http://localhost:3000/media/kitten.jpg

it will search file in root media/kitten.jpg

if you have route :

app.get("/static" ,express.static(__dirname + /public/));

it doesn't means that your files are in static/public. It means that you map static url to public directory

it is describe here: http://expressjs.com/en/starter/static-files.html

Upvotes: 3

Related Questions