将CSS文件链接到PUG文件时出现问题?

时间:2019-05-11 14:42:39

标签: javascript css node.js express pug

我正在尝试使用“链接”语法将CSS文件链接到PUG文件。当我尝试这样做时,出现以下错误消息:

“拒绝应用'http://localhost:3000/CSS/styles.css'中的样式,因为它的MIME类型('text / html')不是受支持的样式表MIME类型,并且启用了严格的MIME检查。”

  • 我尝试过添加属性type =“ text / css”。没用
  • 我尝试使用以下语法验证css文件路径是否正确:
    style
        include ../CSS/styles.css

这有效,并且允许我确定css文件路径正确。但是,我想改用index.pug文件中所示的“ link()”语法。

index.pug头:

    head
        link(rel='stylesheet' href='../CSS/styles.css' type='text/css')

app.js文件:

    const express = require('express');
    const http = require('http');
    const path = require('path');

    const app = express();
    const viewsPath = path.resolve(__dirname, "views");
    app.set("views", viewsPath);
    app.set('view engine', 'pug');

    app.get("/", function (request, response) {
        response.render("index");
    });
    http.createServer(app).listen(3000);

1 个答案:

答案 0 :(得分:2)

您需要将CSS文件存储为static。与Express搭配使用时,最好使用absolute paths

假设您的Dashboard仅包含可以安全地公开到网络的文件。

app.use(express.static('Dashboard'))

// In your html, note absolute path
href='/CSS/styles.css'

您还可以通过指定安装点来考虑virtual path

app.use('/static', express.static('Dashboard'))

// html
href='/static/CSS/styles.css'