Express.js - 减少中间件 - 使用单独文件夹时生成的路径错误

时间:2015-09-12 16:57:55

标签: node.js express less

我使用默认的lessMiddleware设置,其中已编译的 .css文件存储在与 .less文件相同的文件夹中。< / p>

然后我切换到为cssless文件使用单独的文件夹,然后我的.less文件不再编译为.css了(基本上是路径现在都错了 - 请查看底部的调试日志。我做错了什么?

文件夹结构:

  • .....
  • 公共
    • 样式
      • CSS
  • .....
  • app.js

app.js:

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

app.use(express.static(__dirname + "/public"));

app.get('/', function(req, res, next) 
{
    res.render('index.ejs');
});

index.ejs:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="/styles/css/animations.css"/>
        <link rel="stylesheet" type="text/css" href="/styles/css/global.css"/>
        ........
        ............

减少调试日志:

  pathname : /styles/css/animations.css
  source : /Users/mothership/testapp/public/styles/less/styles/css/animations.less
  destination : /Users/mothership/testapp/public/styles/css/styles/css/animations.css
  pathname : /styles/css/global.css
  source : /Users/mothership/testapp/public/styles/less/styles/css/global.less
  destination : /Users/mothership/testapp/public/styles/css/styles/css/global.css

1 个答案:

答案 0 :(得分:0)

原来解决方案如下(可能有一个更干净但我无法找到它):

app.use(lessMiddleware(__dirname + '/public/styles/less', {
    force: true,
    dest: __dirname + '/public',
    debug: true,
    preprocess: {
        path: function(pathname, req) {
          return pathname.replace(/styles\/css\//, '');
        }
      }
}));