首页 > 解决方案 > 使用 Pug 视图引擎时 Express less-middleware 不编译

问题描述

我对使用 Express 和 Pug 很陌生,但我知道这应该很简单。

我试图让我的 Pug 视图使用由 less-middleware 模块编译的 css 文件,但我似乎无法让 less-middleware 将我的 less 文件编译成 css。

index.js

const express = require('express');
const lessMiddleware = require('less-middleware');
const app = express();
...
app.set('view engine', 'pug');
app.set('views','./views');
app.use(lessMiddleware(__dirname + '/public'));
app.use(express.static(__dirname + '/public'));
...

家.pug

html
    head
        title title
        link(rel="stylesheet" type="text/css" href="../public/style.css")

文件结构

/public
    style.less
/src
    index.js
/views
    ...
    home.pug

http://localhost:3000/home显示此错误消息:

Refused to apply style from 'http://localhost:3000/public/style.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

现在我知道这意味着 style.css 文件不存在(我认为),这源于 less-middleware 没有将 less 编译成 css 对吗?

我知道这实际上是其他问题的复本,例如:Express less-middleware not compile but I've tried everything I can and I'm out of idea.

抱歉,如果这真的很愚蠢/简单,但我觉得我现在正试图解决这个问题。任何帮助将非常感激

谢谢

标签: javascriptnode.jsexpresslesspug

解决方案


推荐阅读