Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExpressJs Serve multiple static folders not working

I want to structure my public folder like this:

 - public
     -  frontend
     -  backend
     -  assets   # to serve common css, and js static files for backend/frontend

if the request is prefixed by /admin then I want to serve backend folder, else then the frontend folder this is what I've tried

app.use(function(req, res, next) {
    if (req.url.match('^\/admin')) {
        express.static(__dirname + '/public/admin')(req, res, next);
    } else {
        express.static(__dirname + '/public/frontend')(req, res, next);
    }
});

app.use(express.static(__dirname + '/public/assets'));

app.get('*', function(req, res, next) {
    if (req.url.match('^\/admin')) {
        res.sendFile(__dirname + '/public/admin/app/views/index.html');
    } else {
        res.sendFile(__dirname + '/public/frontend/app/views/index.html');
    }
});

but I can't get the static files from the assets folder, it keeps giving me index.html instead. what is the right approach to make it works ? thx alot

like image 592
user3833083 Avatar asked Mar 15 '23 15:03

user3833083


1 Answers

If I understood your question correctly, I think something like this would be more the way to go.

app.use('/admin', express.static('public/backend'));
app.use('/', express.static('public/frontend'));
like image 155
Bert Vermeire Avatar answered Mar 25 '23 05:03

Bert Vermeire