2012-08-10 14 views
1

我已經在Express中設置了服務器端的LESS編譯,並且它在翡翠中運行正常,而且不會減少佈局。ExpressJS - 在我的佈局上,我的編譯錯誤更少

我的終端:

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

app.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: ENOENTleft_navigator.less無法打開。我把test.less和navigator.less放在同一個目錄下,這沒有任何意義。

LESS在服務器端讓我瘋狂。請有人能幫助我。由於

+0

這是你在找什麼? http://stackoverflow.com/questions/7681407/node-js-fs-stat-throws-enoent-the-operation-completed-successfully – 2012-08-10 06:27:32

+0

nope ..,我的節點可以成功運行,如果我不把LESS樣式錶鏈接在Jade佈局。但是,如果我將LESS樣式錶鏈接放在我的Jade佈局中,它就出現了'錯誤:ENOENT' – LiJung 2012-08-10 06:42:56

+0

如果將它從版面中刪除,是否正確呈現'test.less',或者您是否得到相同的錯誤?另外,請確保您沒有權限問題。 – ebohlman 2012-08-10 16:08:53

回答

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,並且它會自動生成。我使用主/子佈局與玉石和較少這裏如果一個例子將有助於:

https://github.com/JustinBeckwith/ExpressStarter

編碼快樂!

+0

我很感謝您的幫助!最後,我得到了一個解決方案。 :) – LiJung 2012-08-11 10:20:50

+0

它不適用於Express 4.x.x – Srathi00 2015-03-08 16:20:30