Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js: Subpages can't find public statics folder

I'm writing a webapp in Node.js and Express. My subpages (e.g. /reflection/id) cannot find my statics folder. Rather than looking for /stylesheets, /javascripts, etc. (which would be redirected to /public/stylesheets, etc.), it is searching within the subpage, i.e. /reflection/stylesheets.

You can go to the Heroku page (also linked below) and check out the developer console, and you'll see that it's searching the wrong path.

I suspect that the problem exists in my app.js (link), probably around line 24:

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

Any idea what's going on? Am I not declaring the static path correctly? Any insights are appreciated.

Project on Git: https://github.com/gredelston/every6

Heroku deployment, relevant page: http://every6.herokuapp.com/reflection/test

like image 367
Greg Edelston Avatar asked Nov 23 '25 05:11

Greg Edelston


1 Answers

You need to use absolute URLs

<link rel="stylesheet" type="text/css" href="/stylesheets/main.css">

instead of

<link rel="stylesheet" type="text/css" href="stylesheets/main.css">

Otherwise the browser tries to load stylesheets relative to the current URL you are viewing.

like image 133
loganfsmyth Avatar answered Nov 24 '25 17:11

loganfsmyth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!