Search code examples
expresshandlebars.js

static file load on different route expressjs


I am learning expressjs and I faced a problem that I found any solution to it. Basically When I am running my express app I can load all static file (CSS, JS) on root route. (localhost:3000). Now let's say I have another route (/users) so when my URL is localhost:3000/users/ I am loading a handlebar page within I have <link rel='stylesheet' type='text/css' href='css/app.css'/>

I am setting up my static files like this app.use(express.static(path.join(__dirname , '/public')));

It loads all static files on root or index file. So when I am on /users directory, that page is looking for app.css in http://localhost:3000/gifts/css/app.css

So how do I get all the static files from any routes?


Solution

  • This should work by changing the path to

    <link rel='stylesheet' type='text/css' href='/css/app.css'/>