我创建了一个node.js服务器.当我使用端口3000进入localhost时,它只显示没有css或
javascript的文本.我尝试过几种可以解决其他问题的解决方案.但他们没有为我工作.
Node JS keep getting Failed to load resource error message
我的文件顺序是这样的
server.js index.html public css style.css
这是服务器的代码
var express = require('express'); var app = express(); app.get('/',function(req,res) { res.sendFile(__dirname + '/index.html');}); app.use(express.static(__dirname + 'public')); app.listen(3000);
这些是错误
GET http://localhost:3000/ [HTTP/1.1 304 Not Modified 11ms] GET http://localhost:3000/public/css/demo.css [HTTP/1.1 404 Not Found 10ms] GET http://localhost:3000/public/css/themes/popclip.css [HTTP/1.1 404 Not Found 10ms] GET http://localhost:3000/public/css/reveal.min.css [HTTP/1.1 404 Not Found 19ms] GET http://localhost:3000/public/css/theme/default.css [HTTP/1.1 404 Not Found 12ms] GET http://localhost:3000/public/css/jquery.dropdown.css [HTTP/1.1 404 Not Found 11ms] GET http://localhost:3000/node_modules/socket.io/node_modules/socket.io-client/socket.io.js [HTTP/1.1 404 Not Found 10ms] GET http://localhost:3000/public/js/jquery.min.js [HTTP/1.1 404 Not Found 29ms] GET http://localhost:3000/public/js/jquery.popline.js [HTTP/1.1 404 Not Found 28ms] GET http://localhost:3000/public/js/plugins/jquery.popline.link.js [HTTP/1.1 404 Not Found 28ms]
解决方法
这个:
__dirname + 'public'
应该:
__dirname + '/public'
此外,您的html / css中的路径可能不正确,因为__dirname’/ public’将是您的根.因此,除非您有一个目录__dirname’/ public / public’,否则您将需要更改html / css中的路径.