简体   繁体   English

将CSS文件链接到Node.js服务器

[英]Linking css files to node.js server

So I have craeted a node.js server with two routes. 所以我用两个路由创建了一个node.js服务器。 I use the fs to get the html files from the views folder and then append them to the page. 我使用fs从views文件夹获取html文件,然后将其附加到页面。 In those html files I have a normal link to the css file, which does not seem to work. 在这些html文件中,我具有指向css文件的普通链接,该链接似乎无效。 Here is my node.js app: 这是我的node.js应用程序:

var port = 1357;

var http = require('http'),
    path = require('path'),
    mime = require('mime'),
    fs = require('fs');

var app = http.createServer( function(req, res) {

    if (req.url === '/home') {
        fs.readFile('views/index.html', function(err, page) {
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.write(page);
            res.end();
        });
    }

    else if (req.url === '/about') {
        fs.readFile('views/about.html', function(err, page) {
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.write(page);
            res.end();
        });
    }

        else {
            res.writeHead(301,
              {Location: '/home'}
            );
            res.end();
        }
});



app.listen(port);
console.log('Server running on port: ' + port)

In the html files I have: 在html文件中,我有:

<link rel="stylesheet" type="text/css" href="./styles/styles.css">

It does not work. 这是行不通的。 In chrome's console I get "Resource interpreted as Stylesheet but transferred with MIME type text/html. " 在chrome的控制台中,我得到“资源解释为样式表,但以MIME类型text / html传输。”

You defined 2 routes: /home and /about. 您定义了2条路线:/ home和/ about。 You also defined that anything apart from these two routes should default to an HTTP redirect to the /home route, and this is what causes the problem. 您还定义了这两条路由以外的任何内容都应默认使用HTTP重定向到/ home路由,这就是导致问题的原因。

When the browser encounters the link to the css file, it requests the following URL: /styles/styles.css . 当浏览器遇到指向css文件的链接时,它会请求以下URL: /styles/styles.css the server receives this URL and since it doesn't match the two defined routes it will go into the else statement which will send a redirect to /home, so your browser, asking for a css file, will only receive the html page located in /home. 服务器接收到该URL,并且由于它与两个已定义的路由不匹配,它将进入else语句,该语句将发送重定向到/ home,因此,您的浏览器在请求CSS文件时将仅接收位于中的html页面。 /家。

To fix this, you might need to add a new rule for your css file: 要解决此问题,您可能需要为css文件添加新规则:

else if (req.url === '/styles/styles.css') { fs.readFile('styles/styles.css', function(err, page) { res.writeHead(200, {'Content-Type': 'text/css'}); res.write(page); res.end(); }); 否则if(req.url ==='/styles/styles.css'){fs.readFile('styles / styles.css',function(err,page){res.writeHead(200,{'Content-Type' :'text / css'}); res.write(page); res.end();}); } }

Of course, if you have more css files you need to manage a specific folder instead of files. 当然,如果您有更多的CSS文件,则需要管理特定的文件夹而不是文件。 I suppose you're doing this to learn Node, because if you don't you might want to use express which is a Node ready to use web server that will save you lot of time. 我想您正在这样做是为了学习Node,因为如果您不这样做,则可能要使用express ,这是Node可以使用的Web服务器,可以节省大量时间。

When the client (the browser) asks the server for /styles/styles.css the server responds with 301 Moved Permanently and Location: '/home' . 当客户端(浏览器)向服务器请求/styles/styles.css ,服务器将以301 Moved PermanentlyLocation: '/home'响应。

The browser then asks for /home and gets an HTML document, which is not a stylesheet. 然后,浏览器要求输入/home并获取一个HTML文档,该文档不是样式表。

You have to give the browser the stylesheet when it asks for it. 您必须在浏览器要求时提供样式表。

static assets (as in your stylesheets) wont be served automatically. 静态资产(如样式表中的内容)将不会自动投放。 So what happens is that it falls through and lands at the 301 redirect to /home, where you serve text/html. 因此,发生的情况是它掉入并落在301重定向到/ home的位置,在该处提供text / html。 If you want to serve css that way, add a rule req.url==="/styles/styles.css" 如果您想通过这种方式提供CSS,请添加规则req.url==="/styles/styles.css"

Generally, I would recommend using a routing lib like express or koa . 通常,我建议使用路由库,例如expresskoa Or as minimum, connect . 或至少connect They make it easy to hook in features called middleware and enable you to make everything in a directory (like /public) serve static content with one rule. 它们使钩挂称为中间件的功能变得容易,并使您能够使目录中的所有内容(如/ public)都通过一条规则提供静态内容。

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

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