Node.js-express-jade-编译 SASS/LESS

发布于 2024-10-06 00:27:46 字数 842 浏览 11 评论 0原文

有人有 Nodejs - Express - SASS/LESS 的真正新手指南吗?我无法让这个工作。我现在的例子是尽可能简单的。

var express = require('express'),
    less = require('less'),
    app = express.createServer();

var pub_dir = __dirname + '/public';

app.configure(function(){ 
    app.use(express.compiler({ src: pub_dir, enable: ['less'] }));
    app.use(express.staticProvider( pub_dir ));
};

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

文件 style.css.less 位于 pub_dir 中,我可以直接访问它,并且样式是

@brand_color: #4D926F;
body {
  color: @brand_color;
}

As far据我了解,编译应该在启动时进行,并且将在 src-dir 中生成一个 css 文件,因为未指定 dest。
我的服务器运行良好,但无论我尝试了多少种方法来弄乱 LESS/SASS 文件的目录名、目录和名称(及其各自的 LESS/SASS 内容),我都无法正常工作。该死!帮助。

Anyone have a really newbie guide to nodejs - express - SASS/LESS? I have not been able to get this working. The example I have now is a bareboned as possible..

var express = require('express'),
    less = require('less'),
    app = express.createServer();

var pub_dir = __dirname + '/public';

app.configure(function(){ 
    app.use(express.compiler({ src: pub_dir, enable: ['less'] }));
    app.use(express.staticProvider( pub_dir ));
};

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

The file style.css.less is located in pub_dir, I can access that directly, and the styling is

@brand_color: #4D926F;
body {
  color: @brand_color;
}

As far as I understand, the compilation is supposed to happen on start-up, and a css file will be generated in the src-dir, as dest is not specified.
My server runs fine, but regardless of how many ways I have tried messing around with the dirnames, directories and names of the LESS/SASS files (and their respective LESS/SASS content) I cannot get this working. Darn! Help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无可置疑 2024-10-13 00:27:46

我也是一个试图获得此设置的新手。我尝试了一些发现的片段,直到我终于注意到express有一个“express”命令来设置一个新项目。

尝试 express -c less 创建一个使用 LESS 作为 CSS 引擎的示例项目。

这应该创建所需的目录。新的 css 文件将从您的静态目录提供。

配置是:

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'hbs');
  app.use(express.bodyDecoder());
  app.use(express.methodOverride());
  app.use(express.compiler({ src: __dirname + '/public', enable: ['less'] }));
  app.use(app.router);
  app.use(express.staticProvider(__dirname + '/public'));
});

I'm also a newb trying to get this setup. I have tried a few snippets I found until I finally noticed that express has an 'express' command that sets up a new project.

Try express -c less to create a sample project with LESS as the CSS engine.

This should create the required directories. The new css files will be served from your static directory.

The configuration is:

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'hbs');
  app.use(express.bodyDecoder());
  app.use(express.methodOverride());
  app.use(express.compiler({ src: __dirname + '/public', enable: ['less'] }));
  app.use(app.router);
  app.use(express.staticProvider(__dirname + '/public'));
});
闻呓 2024-10-13 00:27:46

您的设置是 readymade 的标准设置。确保您的系统上安装了 less 编译器。

npm install lessjs Readymade

然后将以下内容添加到您的 server.js

app.use(require('readymade').middleware({root: "public"}));

Your setup is the standard setup for readymade. Make sure that less compiler is installed on your system though.

npm install lessjs readymade

And then add the following to your server.js

app.use(require('readymade').middleware({root: "public"}));

随梦而飞# 2024-10-13 00:27:46

这是一个类似的问题:
Node.js + Express.js。如何渲染更少的 css?

您还可以查看 ExpressJS 网站 上的指南

Here's a similar question:
Node.js + Express.js. How to RENDER less css?

you can also check out the guide on the ExpressJS Website

猫烠⑼条掵仅有一顆心 2024-10-13 00:27:46

我正在研究express 4.x并使用SASS。这是我用于样式的片段部分(注意评论):

var express = require('express'),
    // ... other packages
    sass = require('node-sass');

// ...
var app = express();
// ...
// Commented this default express generator line:
// app.use(require('stylus').middleware(path.join(__dirname, 'public')));
//
// Because of some bug the node-sass (http://git.io/eItWzA) does not scan the correct folders,
// so I have both .scss and final .css in one /public/css/ folder
app.use(
    sass.middleware({
        src: __dirname + '/public/', // where the sass files are 
        dest: __dirname + '/public/', // where css should go
    })
);

// ... rest of app

这是它的要点: http://git .io/Vr9KJA

I'm working on express 4.x and using SASS. Here is a snippet part I'm using for styles (mind the comments):

var express = require('express'),
    // ... other packages
    sass = require('node-sass');

// ...
var app = express();
// ...
// Commented this default express generator line:
// app.use(require('stylus').middleware(path.join(__dirname, 'public')));
//
// Because of some bug the node-sass (http://git.io/eItWzA) does not scan the correct folders,
// so I have both .scss and final .css in one /public/css/ folder
app.use(
    sass.middleware({
        src: __dirname + '/public/', // where the sass files are 
        dest: __dirname + '/public/', // where css should go
    })
);

// ... rest of app

Here is a gist for it: http://git.io/Vr9KJA

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文