Express:提供静态文件不起作用

时间:2016-09-26 23:22:46

标签: node.js express

这是我的项目结构:

-backend
 -server.js
-www
 -index.html
 -app.css
 -bundle.js

我遇到的问题是Express无法提供静态文件,其网址有2个以上的斜杠

localhost:3000/product/1

但它适用于只有1个斜杠的网址:

localhost:3000/register
localhost:3000/home

问题发生在它总是试图获得

  

本地主机:3000 /产品/ 1 / app.css

  

本地主机:3000 /产品/ 1 / bundle.js

  

本地主机:3000 /产品/ 1

这是我在server.js中的配置:

app.use(express.static(path.resolve(__dirname, '../www')));
app.get('*', function (request, response){
  response.sendFile(path.resolve(__dirname, '../www', 'index.html'))
});

1 个答案:

答案 0 :(得分:0)

我在index.html中使用“./bundle.js”和“./app.css”。更改为“/bundle.js”和“/app.cs”并使其正常工作。感谢帖子下的评论!

相关问题