Node.js的为什么页面无法加载css和javascript文件?

我创建了一个node.js服务器.当我使用端口3000进入localhost时,它只显示没有css或 javascript的文本.我尝试过几种可以解决其他问题的解决方案.但他们没有为我工作.

Node JS keep getting Failed to load resource error message

static files with express.js

Can not get CSS file

我的文件顺序是这样的

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中的路径.

相关文章

这篇文章主要介绍“基于nodejs的ssh2怎么实现自动化部署”的...
本文小编为大家详细介绍“nodejs怎么实现目录不存在自动创建...
这篇“如何把nodejs数据传到前端”文章的知识点大部分人都不...
本文小编为大家详细介绍“nodejs如何实现定时删除文件”,内...
这篇文章主要讲解了“nodejs安装模块卡住不动怎么解决”,文...
今天小编给大家分享一下如何检测nodejs有没有安装成功的相关...