Home > OS >  Express Static files not working on Catch-all address
Express Static files not working on Catch-all address

Time:11-18

I have a 404 page on my website, but on a nested page (for example test1/test2) static files aren‘t working.

Below is an example for a page with only one part. The css file works normal.

app.get("/:part1", (req, res) => {
  res.render("404")
});

If I go to domain/test i will get a page with the static style.css: https://i.stack.imgur.com/UrmLf.jpg

Below is an example for a page two parts. The css file doesn‘t load.

app.get("/:part1/:part2", (req, res) => {
  res.render("404")
});

Now if I go to domain/test1/test2 I will still get the message, but not the css: https://i.stack.imgur.com/70SHn.jpg

CodePudding user response:

use the express.static method for static files :

const path = require('path')
app.use('/static', express.static(path.join(__dirname, 'public')))

CodePudding user response:

I found my mistake. In the html file I needed to change:

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

to:

<link href="/style.css" rel="stylesheet" type="text/css"/>
  • Related