Express.Static not working for subdirectory
Asked Answered
G

4

9

Express.static is working great for any view in root directory, but it breaks when I add a subdirectory.

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

Works:

//Homepage
router.get('/', isAuthenticated, function (req, res, next) {
    res.render('index.hbs', {user: req.user, message:req.flash('message')});      //add add'l data {title:'Express', addlData:'SomeData', layout:'layout'}
});

Doesn't Work:

//Organization Profile
router.get('/orgprofile/:username', function(req,res,next){
    User.findOne({username:req.params.username}, function(err,docs){
        res.render('orgprofile.hbs',{orgdetails:docs});
    });
});

I'm guessing __dirname is changed to whatever directory the get request is made from, but I just want to use the root (/public) directory for all static requests.

Grab answered 4/1, 2016 at 18:2 Comment(1)
__dirname is not changed per request. To resolve this, please show the URL you wish to be served as a static request and the directory structure where you want the static request to come from. And, also, express.static() has nothing to do with res.render() calls if that is what your problem is. Those are normal file operations that go through the template layout engine, not through the middleware code path where express.static() is.Sensual
S
15

Use it this way:

app.use('/', express.static(__dirname + '/public'));
app.use('/orgprofile', express.static(__dirname + '/public'));
Skerrick answered 4/1, 2016 at 18:12 Comment(4)
Thanks! Is it basically saying "replace '/orgprofile' with (__dirname + '/public')" ?Grab
Yes, its setting all the request coming on the handle to be served from the directory.Skerrick
Downvoted because you have to manually create routes for every subfolder you have.Austroasiatic
Hey @WonderDog, the answer is based on the limits by express. I understand that this is not a great solution. Might be better to open a feature request with Express than downvoting the only existing solution.Skerrick
T
3

Problem is not caused by express.static() expression. It might be due to wrong href value. express.static():

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

Reference:

<link rel="stylesheet" type="text/css" href="/stylesheets/main.css">
Trakas answered 27/3, 2019 at 10:5 Comment(1)
This is a better approach than the accepted answer. Try not to repeat yourselfInspect
M
0

I had the same problem. If I use an URL like http://localhost:8080/postcreate/personal it would fail because of wrong value of __dirname. The URLs in the layout file for css, js files or image sources get the wrong prefix. I did solve this by using: proces.env.PWD instead. Now its always using the project folder and that's what I want. The project folder is my root. Same like .NET projects work. The problem with me was that I set express.static in a seperate app folder.

Melda answered 27/10, 2022 at 22:36 Comment(0)
C
0

I have used this :

appRouter.all('/*', (req, res) => { res.sendFile('public'.concat(req.path), { root: process.cwd() }); });
Capacitor answered 10/9, 2024 at 6:39 Comment(1)
Hi, you forgot to explain how this can address the question. Be sure to read how to answer. Thank youAltair

© 2022 - 2025 — McMap. All rights reserved.