Express较少的中间件无法编译 [英] Express less-middleware not compiling

查看:54
本文介绍了Express较少的中间件无法编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我编译我的CSS吗?我不知道为什么我的观点没有得到较少的编译

Can someone please help me get my css compiling. I don't know why my view is not getting the compiled less

// MIDDLEWARE
var express = require('express');
var less = require('less-middleware');
var app = express();

// LESS COMPILER
app.use(less('source/less', {
  "dest": 'public/stylesheets',
  "pathRoot": __dirname,
  "force": true,
  "render": {
    "yuicompress": true
  }
}));

// APP CONFIG
app.use(express.static(__dirname + '/public'));
app.set('views', 'views')
app.set('view engine', 'jade');

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

// PAGE NOT FOUND
app.use(function(req, res, next) {
  res.status(404).send('Sorry cant find that!');
});

// WEB SERVER
var server = app.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;
  console.log('Example app listening at http://%s:%s', host, port);
});

Index.jade

html
  head
    title Hello
    link(src="stylesheets/global.css" type="text/css")
  body
    h1 Welcome

推荐答案

您可以在

You can read about how to set the src and dest dirs for your less-middleware to different directories at https://github.com/emberfeather/less.js-middleware/wiki/Examples. But the upshot is this: To keep things relatively straightforward, the relative path of your source and dest must be identical.

我建议将较少的源文件移出 public ,以简化此操作,并且因为无论如何都不应按原样提供这些文件.

I would recommend moving your less source files out of public to make this easier and because they're not supposed to be served as-is anyway.

在与您的 public 目录相同的目录中创建 less-src 目录.在 less-src 内,创建一个 stylesheets 目录,并将您的 global.less 放在其中.然后将您较少使用的中间件更改为此:

Create a less-src dir in the same directory that has your public directory. Inside less-src, create a stylesheets directory and put your global.less in there. Then change your less middleware use to this:

app.use(lessMiddleware(__dirname + '/less-src', 
    {dest: __dirname + '/public'}));

那将起作用.

如果您不喜欢在 less-src 中有一个 stylesheets 目录,那么您需要在上面的链接中查看有关预处理较少路径的材料.对我而言,这在源代码布局方面以极少的收益换取了代码复杂性.但是,如果您觉得这值得,那么您就可以做到这一点.

If you dislike having a stylesheets directory in less-src, then you need to look at the material about preprocessing the less path at the the link above. To me, that trades code complexity for very little gain in terms of source code layout. But if it seems worth it to you, then that's how you can do it.

这篇关于Express较少的中间件无法编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆