简体   繁体   中英

Loading a custom CSS Handler for Node.JS

I have a basic Node.JS server setup as I'm doing a new project for work. I've written and optimizer function that strips unnecessary things like tabs, newlines and comments from HTML, JavaScript and CSS.

For some odd reason when I added my <link type="text/css" href="/css/bootstrap.mins.css"> to my head it's not loading or showing up in the Google Network tab.

The main HTML is loading just not the CSS.

Server:

app.get("/css/:file",function(req,res){
    logger.log("CSS File");
    res.writeHead(200, {"Content-Type": "text/css"});
    res.write(fs.readFileSync(appRoot + "/css/" + req.params.file,"utf8").optimize());
    res.end();
});

There is no nested CSS files, all in one directory. This, in theory, should work but it isn't. It loads and is processed when I go to the direct URL. Just not from the LINK .

Google控制台元素标签中的HTML大纲。

Loading the HTML Page: HTML未加载CSS文件。

Loading the CSS Directly; It does preprocess: RAW链接已加载并已预处理。

Okay, after further study I found that my code above did work. I had 1 slight failure though. I missed the rel tag in the actual link. The CSS loads fine and optimized after this.

<head>
    <title>In House Tool</title>
    <link rel="stylesheet" type="text/css" href="/css/bootstrap.min.css">
</head>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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