简体   繁体   English

node.js - 图像和 css 不起作用

[英]node.js - images and css don't work

I'm a newbie in node.js and I just made a chat based on socket.io example.我是 node.js 的新手,我刚刚基于 socket.io 示例进行了聊天。 I did a login form on the index of my application but the images and css are not working when I access to my application on localhost:3000.我在我的应用程序的索引上做了一个登录表单,但是当我在 localhost:3000 上访问我的应用程序时,图像和 css 不起作用。 However, when I launch my index.html directly on my browser the css and the images are correctly loaded.但是,当我直接在浏览器上启动 index.html 时,css 和图像已正确加载。

That's the tree of my application : Tree view那是我的应用程序的树:树视图

And that is what I see when I launch my node js server and I access to localhost:myPort: index.html on localhost这就是我在启动节点 js 服务器并访问 localhost:myPort: index.html on localhost时所看到的

That the head of index.html : index.html 的头部:

<meta charset="UTF-8">
<title>Khoya Talk</title>
<link rel="shortcut icon" href="public/assets/images/favicon.ico" type="image/x-icon">
<link rel="icon" href="public/assets/images/favicon.ico" type="image/x-icon">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">
<!-- Optional theme -->
<!--<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css" integrity="sha384-aUGj/X2zp5rLCbBxumKTCw2Z50WgIr1vs/PFN4praOTvYXWlVyh2UtNUU0KAUhAX" crossorigin="anonymous">-->
<link href='https://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" integrity="sha512-K1qjQ+NcF2TYO/eI3M6v8EiNYZfA95pQumfvcVrTHtwQVDG+aHRqLi/ETn2uB+1JqwYqVG3LIvdm9lj6imS/pQ==" crossorigin="anonymous"></script>
<link rel="stylesheet" type="text/css" href="public/css/style.css">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->

Thank you for your help.感谢您的帮助。

Add / before all public and:在所有公共和之前添加/

App.js应用程序.js

app.use(express.static(__dirname + '/public'));

index.html索引.html

<link rel="shortcut icon" href="/assets/images/favicon.ico" type="image/x-icon">

And make this for all path.并为所有路径制作这个。 And src property in images:和图像中的src属性:

<img src="/assets/images/halftone.png">

When you load the page in your browser, check the developer tools / debugger to see whats happening.当您在浏览器中加载页面时,请检查开发人员工具/调试器以查看发生了什么。

If it cant find the css (or the images), its because you need to use Express like this (in your js file): At the top add:如果它找不到 css(或图像),那是因为您需要像这样使用 Express(在您的 js 文件中):在顶部添加:

 var express = require("express");
 var app     = express();

Then add folders with the content like this:然后添加内容如下的文件夹:

 app.use(express.static(__dirname + '/public'));

Apparently, you have to use the / with the static directory显然,您必须将 / 与静态目录一起使用

app.use(express.static(__dirname + '/public/'));

Then in your CSS: background-image('img/pic.png') where img is the folder you store your images in, and make sure it is in the public folder.然后在您的 CSS 中: background-image('img/pic.png') 其中 img 是您存储图像的文件夹,并确保它位于公共文件夹中。

Hope that helps.希望有帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM