Use images and js from different ExpressJs locations: Node


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?


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

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/