我遇到了很多类似这样的问题,但看起来我没有做错什么。问题是我的静态文件无法加载。
文件夹结构:
/client
index.html
/assets
/css
main.css
/server
app.js
app.js:
var assetsPath = path.join(__dirname, '../client/assets');
app.use(express.static('assetsPath'));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, '../client/' + 'index.html'));
});
app.listen(PORT, function () {
console.log('\nListening on port 8080!');
});
index.html
<link rel="stylesheet" href="/css/main.css">
然而,当在浏览器中加载页面时,http://localhost:8080/css/main.css
返回404错误。
是否有遗漏的内容?
谢谢!
PS:使用express ^4.13.4和node v5.10.1