Express serving static asset files relative to route instead of absolute

In express, I have the following route:

app.get('/story/:username', function(req, res){


And before that, I serve any static files located in /public


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


Now I figured this was serving the root directory so that when I visited the path http://localhost:8080/story/user17

it could still pull my static files like css/main.css

from http://localhost:8080/css/main.css

. Instead, when I tried to load the page and checked the console, I saw this error all the time:

GET http://localhost:8080/story/css/main.css (404)


for all my static files. Why is it trying to load all my static files from the relative directory instead of the absolute root and how to fix it?


source to share

1 answer

Change the path to your CSS file in dashboard.ejs

from css/main.css

to ../css/main.css

so that it can be found on the page that the browser sees as/story/someUser



All Articles