使用Express减少中间件

8

我正在尝试在我的nodejs项目中设置less,当我从浏览器请求我的页面时,less-middleware显示了错误的路径和目标路径。当我请求例如'/public/css/index.css'时,它将'/public/css/'添加到源路径中。以下是我的配置:

app.use(lessMiddleware(__dirname+'/server/less',{
    debug: true,
    dest: __dirname+'/public/css',
    once: true
}));

调试屏幕显示:

pathname : /public/css/index.css
source : D\Work\project\server\less\public\css\index.less
destination : D\Work\project\public\css\public\css\index.css

即使我已经设定好了目标路径,但我的问题出在源文件上,并且我不想把我的less文件放到public文件夹中。有没有办法可以从编译后的less文件中删除/public/css呢? 我已经尝试添加以下内容:
'preprocess.path': function(pathname, req){
    console.log(pathname);
}

但是console.log从未显示。另外,也许有一种方法可以不编译用户请求的每个CSS文件,而只编译我在less文件夹中拥有的那些less文件。
附注:我使用的每个模块都是"*"版本。

2个回答

5
您可以尝试在浏览器中访问"/css/style.css"来解决此问题。
app.use(lessMiddleware({
  src: __dirname+"/server/less",
  dest: __dirname+"/public/css",
  prefix: "/css",
  // force true recompiles on every request... not the
  // best for production, but fine in debug while working
  // through changes
  force: true,
  debug: true
}));

我尝试了你的代码,但它显示了"path.join必须是一个字符串"。 - Honchar Denys
我编辑了代码,在两种情况下删除了加号前/后的空格。听起来Less在幕后使用path.join(),并且对其中一个生成的路径不满意。 - Michael Blankenship

1
实际上,这很简单。
app.use(lessMiddleware(__dirname+'/server/less/',{
    debug: true,
    dest: __dirname,
    force: true
}));

所以现在在文件夹/server/less中,我们创建文件夹public/css,一切都正常引入。如果有admin/css文件夹也可以使用。诀窍是在/server/less文件夹中创建文件夹。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接