1

我已经在 Express 的服务器端设置了编译 LESS,它在 Jade 中正常工作,而没有减少布局。

我的终端:

if(err) throw err;
                ^
Error: ENOENT, open '/Users/lijung/Documents/Project/clubond/public/stylesheets/left_navigator.less'

应用程序.js:

/**
 * Module dependencies.
 */

var express = require('express')
    , path = require('path')
    , club = require('./routes/club')
    , less = require('less')
    , fs = require('fs');

var app = module.exports = express.createServer();

// Configuration

app.configure(function(){
    var RedisStore = require('connect-redis')(express);
    app.use(express.cookieParser());
    app.use(express.session({ secret: "william", store: new RedisStore }));
    app.use(express.logger());
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(app.router);
    app.set('view options', { layout: false });
    app.use(express.static(path.join(__dirname, 'public')));
});


app.configure('development', function(){
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
    app.use(express.errorHandler()); 
});

// Routes
//index-in-layout
app.get('/club/:id', club.chkExist, club.getDataById, site.clubPage);

//compile less
app.get("*.less", function(req, res) {
    var path = __dirname + req.url;
    fs.readFile(path, "utf8", function(err, data) {
        if(err) throw err;
        less.render(data, function(err, css) {
    if (err) throw err;
        res.header("Content-type", "text/css");
        res.send(css);
     });
   });
}); 


app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);  

我将布局放在名为index_in_layout的视图中:

!!! 5
html
head
    title= title
    script(src='/javascripts/jquery.min.js')
    link(rel="stylesheet", href="/stylesheets/index.css")
    link(rel="stylesheet",type='text/css', href="/public/stylesheets/left_navigator.less")
    script(src='/javascripts/index_in.js')
    block script
body

index.jade:

extends ./index_in_layout

block script

    script(src='/javascripts/new_club.js')
    script(src='/javascripts/new_bond.js')
    script(src='/javascripts/new_event.js')
    script(src='/javascripts/popup.js')
    script(src='/javascripts/list_clubs.js')
    script(src='/javascripts/list_bonds.js')
    script(src='/javascripts/list_events.js')
    link(rel='stylesheet', type='text/css', href='/public/stylesheets/test.less')


block body  

终端一直告诉我Error: ENOENT,我的left_navigator.less无法打开。我把 test.less 和 navigator.less 放在同一个目录下,没有意义。

服务器端的 LESS 让我发疯。有人可以帮帮我吗?谢谢

4

1 回答 1

1

除非我遗漏了什么,否则你真的不需要通过这些英雄事迹来减少工作:-) 通常你只需要在你的 app.configure 调用中添加一行,如下所示:

app.configure(function() {
    app.set('views', __dirname + '/views');
    ...
    app.use(express.compiler({ src: __dirname + '/public', enable: ['less'] }));
    app.use(connect.static(__dirname + '/public'));
    app.use(app.router);
});

如果您这样做,则不需要 *.less 文件的特殊路径。您只需在公用文件夹中请求具有相同名称的 *.css,它就会自动生成。如果一个例子有帮助,我在这里使用带有翡翠和 LESS 的主/子布局:

https://github.com/JustinBeckwith/ExpressStarter

快乐编码!

于 2012-08-11T08:00:02.187 回答