Change to
<link rel="stylesheet" href="/style/not-found.css" >.
You want a path that is relative to the public directory that express.static()
has as its root.
But may u please explain me in case href="./style/not-found.css" why it's works correctly when user enter : "localhost:3000/5" but not work on "localhost:3000/products/5" (I mean loading css successfully)
When the link your HTML page does not start with http://
or with /
, then it is considered a path-relative link and the browser will take the path of the page and combine it with the URL in the link to make a full URL before sending it to the server. So, when you have this:
href="./style/not-found.css"
and the page URL is this:
http://localhost:3000/products/something
The browser will end up requesting:
http://localhost:3000/products/style/not-found.css
And, your server won't know what to do with that. On the other hand, when you change the <style>
tag to this:
href="/style/not-found.css"
Then, your URL starts with a /
so the only thing the browser will add to it is the domain and the browser will request:
http://localhost:3000/style/not-found.css
which will work.
So, when you use a path like:
http://localhost:3000/5
Then, the path for that is just /
so when you combine /
with ./style/not-found.css
, the browser will end up requesting
http://localhost:3000/stye/not-found.css
and it will work because the path was a root path. So, it doesn't work for pages that are not at the top level. This is why your static resource URLs should always be path absolute (start with a /
) so they don't depend upon the path of the hosting page.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…