简体   繁体   中英

ExpressJS application doesn't load CSS file

My ExpressJS web app only loads HTML file but CSS rules are not applied. I linked the HTML file with the CSS file and also added express.static(path.join(__dirname, 'css')) in app.js .

This is my project tree:

app.js
/views
- index.html
/css
- style.css

How can I fix this?

Make sure to add app.use() . All you are doing now is

express.static(path.join(__dirname, 'css'))

What you should do is

app.use(express.static(path.join(__dirname, 'css')))

Also make sure you are referencing the stylesheet in your HTML!

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

The issue was the tag in the HTML file. I had src='./css/style.css' but using src='/style.css' fixed it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM