2

I am using the below code to serve statics files(js, images, css):

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

But now the images have been moved to some different location in the project i.e. outside the public directory (lets say. images).

So how do I differentiate between images and js,css files and serve them from different locations?

EDIT

I guess I have to read the request headers and then decide where to route the request. But looks a little dirty to me.

2 Answers 2

5

Try with two separate endpoints:

import express from "express"; import {join} from 'path'; const app = express(); const publicPath = express.static(join(__dirname, '../public/')); const publicImages = express.static(join(__dirname, '../images/')); app.use('/public', publicPath); app.use('/images', publicImages); app.listen(3400, function() { console.log("Express server listening on port 3400"); }); 

Then you use it like this: http://localhost:3400/images/ and http://localhost:3400/public/

Sign up to request clarification or add additional context in comments.

Comments

0

In that case you just need to add one more static file configuration

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

And it will be served as

http://example.com/files-from-images-folder

3 Comments

Tried, but now my js and css files not be served. Also the link doesn't look right
is this images folder inside "public" folder?
If you use app.use(express.static(path.join(__dirname, 'images'))); you may add your file in /yourproject/images/* :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.