简体   繁体   English

为什么本地.css文件和.js文件未链接到index.html文件?

[英]Why are the local .css files and .js file not linked to the index.html file?

I am using node.js and trying send the .html file contains these tags: 我正在使用node.js并尝试发送包含以下标记的.html文件:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="description" content="This is my personal profile website" />
<link rel="stylesheet" type="text/css" href="profile_design.css" />
<link rel="stylesheet" href="jquery.fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.css" type="text/css" media="screen" />

<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="jquery.fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
<script type="text/javascript" src="profile.js"></script>

Here is the HTTP server in node.js : 这是node.js中的HTTP服务器:

var fs = require('fs');
var http =  require ('http');
http.createServer(function (request, response) {

    console.log('request starting...');

    fs.readFile('C:/Users/.../index.html', function(error, content) {
        if (error) {

            response.writeHead(500);
            response.end();
        } else {
            //response.writeHead(200, { 'Content-Type': 'text/html' });
            response.end(content, 'utf-8');
        }

        console.log(error);
    });

}).listen(8080);

The problem is that the index.html file is return in good manner but all the JavaScript files and the CSS are not sent correctly to the client side. 问题在于index.html文件以良好的方式返回,但是所有JavaScript文件和CSS均未正确发送到客户端。 The question is how to send those .js and .css files? 问题是如何发送那些.js和.css文件?

I faced the same problem, but did not want to use any external modules or frameworks before I understand what's going on under the hood. 我遇到了同样的问题,但是在了解幕后情况之前,我不想使用任何外部模块或框架。 So, this is how I've solved the issue for CSS file for myself. 因此,这就是我自己解决CSS文件问题的方法。 Hopefully it will be useful for someone. 希望它对某人有用。

Whenever I have a request for /style.css I call this simple function: 每当我请求/style.css我都会调用此简单函数:

function style(response) {

  console.log("Request handler 'style' was called.");

  fs.readFile("style.css", function(error, file) {

    if(error) {

        response.writeHead(500, {"Content-Type": "text/plain"});

        response.write(error + "\n");

        response.end();

    } else {

        response.writeHead(200, {"Content-Type": "text/css"});

        response.write(file);

        response.end();

    }

  });

}

Basically, we read our static files and send them back upon request. 基本上,我们读取静态文件,然后根据要求将其发送回去。 Note that you have to take care about routing your requests. 请注意,您必须注意路由请求。

The browser tries to get the .css and .js resource files via port 8080, thorugh your node.js server. 浏览器尝试通过node.js服务器的端口8080获取.css和.js资源文件。 However, your server won't serv any of them but the index.html file irrespectively of the request. 但是,无论请求是什么,您的服务器都不会提供任何服务,但只能提供index.html文件。

Advice: Use the express.js plugin and set up a bit more complex routing . 建议:使用express.js插件并设置更复杂的路由

您需要有一个处理程序来处理诸如node-paperboynode-static之类的资产,或者使您自己的资产。

I am not sure that this answer your question or not, but in my case I add all my css and js files in view folder, then I added the following in my server.js 我不确定这是否回答了您的问题,但就我而言,我将所有的css和js文件添加到了view文件夹中,然后在server.js中添加了以下内容

server.use(express.static(__dirname+'/view/')); server.use(express.static(__目录名+ '/视图/'));

this solved the issue for me 这为我解决了这个问题

easy fix for this issue, is to add your style.css code at the end of your html doc (index.html), right before the ending body tag. 解决此问题的简单方法是,在html文档(index.html)的末尾,即body标记之前,添加style.css代码。 This actually worked in my case. 在我看来,这确实有效。 Just add: 只需添加:

<style type="text/css"> "your css code goes here" </style>

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

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