gpt4 book ai didi

javascript - ExpressJS - 我的 Jade 布局中的编译错误较少

转载 作者:太空宇宙 更新时间:2023-11-04 01:14:37 25 4
gpt4 key购买 nike

我已经在 Express 的服务器端设置了编译 LESS,它在 jade 中可以正常工作,而无需在布局中放入 less。

我的终端:

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 的 View 中:

!!! 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

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

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

最佳答案

除非我遗漏了一些东西,否则你真的不需要通过这些英雄行为来减少工作量:-)通常你只需要在你的 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 文件的特殊路由。您只需在公共(public)文件夹中请求同名的 *.css,它就会自动生成。如果示例有帮助的话,我在这里将主/子布局与 jade 和 LESS 一起使用:

https://github.com/JustinBeckwith/ExpressStarter

祝你编码愉快!

关于javascript - ExpressJS - 我的 Jade 布局中的编译错误较少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11895524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com