简体   繁体   English

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

[英]Node.js. Why is the page cannot load css and javascript file?

i have created a node.js server. 我创建了一个node.js服务器。 When i entered the localhost using port 3000, it displayed text only without css or javascript. 当我使用端口3000进入localhost时,它只显示没有css或javascript的文本。 I have tried several solutions that have worked on others. 我尝试过几种可以解决其他问题的解决方案。 but they didn't worked for me. 但他们没有为我工作。

Node JS keep getting Failed to load resource error message Node JS不断加载资源错误消息

static files with express.js 使用express.js的静态文件

Can not get CSS file 无法获取CSS文件

my file order is like this 我的文件顺序是这样的

server.js
index.html
 public
  css
   style.css

This is the code for the server 这是服务器的代码

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);

These are the errors 这些是错误

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]

This: 这个:

__dirname + 'public'

Should be: 应该:

__dirname + '/public'

Also it looks like your paths in your html/css may not be correct since __dirname + '/public' will be your root . 此外,您的html / css中的路径可能不正确,因为__dirname + '/public'将是您的 So unless you have a directory __dirname + '/public/public' you will have to change the paths in your html/css. 因此,除非您有一个目录__dirname + '/public/public' ,否则您将需要更改html / css中的路径。

Problem solved. 问题解决了。

The problem was in my index.html file. 问题出在我的index.html文件中。 Originally I thought that when i created a new folder and put in all the css and js files there, I needed to add the 'public' in all the src. 最初我以为当我创建一个新文件夹并放入所有css和js文件时,我需要在所有src中添加'public'。 Like this 像这样

<script src="public/js/jquery.min.js"></script>
<script src="public/js/jquery.popline.js"></script>

But instead, I just have to do it without the 'public' folder like this 但相反,我只需要在没有像这样的'public'文件夹的情况下这样做

<script src="js/jquery.min.js"></script>
<script src="js/jquery.popline.js"></script>

and add '/' to the __dirname + 'public' to __dirname + '/public' like mscdex suggested. 并将__dirname + 'public'__dirname + '/public'就像mscdex建议的那样。

After trying a few items, I just wanted to list my working results in case it helps anyone else. 在尝试了几个项目之后,我只是想列出我的工作成果,以防它帮助其他人。

Lines of code I had tried but failed to get working for me: 我曾尝试但未能为我工作的代码行:

app.use('/css', express.static(__dirname + '/public'));
app.use(express.static(__dirname + '/public'));
app.use(express.static(path.join(__dirname, '../public')));

My project was in folder 'project' which contained 'public' folder, 'views' folder, and server.js file. 我的项目位于'project'文件夹中,其中包含'public'文件夹,'views'文件夹和server.js文件。 Node, Express and Handlebars were used to render the pages. Node,Express和Handlebars用于渲染页面。 In 'public' was a folder 'css' where my style.css file was saved. 在'public'中是一个文件夹'css',我的style.css文件已保存在其中。

On server.js I used 在我使用的server.js上

var express = require('express');
var app = express();
app.use(express.static('public'));

On my main.handlebars file in 'views' > 'layouts' I had in the section 在我的main.handlebars文件的'views'>'layouts'中,我在该部分中有

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

With those in place, starting up a local instance worked and no '404' error message when loading the stylesheet. 有了这些,启动本地实例工作,加载样式表时没有'404'错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用 node.js 将文件中的数据插入 mongodb。 数据插入但本地主机页面未加载显示我的结束消息? - Trying to insert data from a file into mongodb using node.js. Data inserts but the localhost page does not load showing my end message? Node.js的 如何限制对未登录用户的页面访问? - Node.js. How restrict page access to unlogged users? file.open不是node.js中的函数。 文件读取 - file.open is not a function in node.js. file reading 为什么我不能加载CSS文件和JavaScript文件? - Why I cannot load the CSS file and JavaScript file? 在Node.js中加载客户端Javascript和CSS - Load client Javascript and CSS in Node.js 通过使用节点调度(Node.js)在 javascript 中应用服务器时区来运行后台服务。 - Running a backround service by applying server Timezone in javascript using node-schedule (Node.js.) node + html页面加载+ javascript,无法加载main.js文件 - node + html page load+ javascript, can't load main.js file AWS Lambda Node.js。 第二次调用后出现mysql错误“调用退出后无法排队查询”: - AWS Lambda Node.js. mysql Error "Cannot enqueue Query after invoking quit" on second call onwards: Node.js的 错误:模块不是函数 - Node.js. Error: module is not a function node.js中的回调函数。 TypeError:无法调用未定义的方法“ emit” - Callback function in node.js. TypeError: Cannot call method 'emit' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM