Your original code wasn’t wrong, it just didn’t align with the style sheet link in index.html
. You have two choices: change the style sheet link or change your route to the static files. According to the docs, you can use
app.use(express.static(__dirname + "/public"));
and serve style.css
from /style.css
or you can use
app.use('/public', express.static(__dirname + "/public"));
to serve the files in the directory public
relative to the project’s working directory from the URL /public
relative to the root URL (in other words, /public/style.css
as both file location and URL). express.static()
just says look in this directory for the static files; the directory/file name has no bearing on the URL that you may choose to assign to the static files.
Or you could change the style sheet link to style.css
instead of /public/style.css
, which is what I did while debugging.