【问题标题】:express + stylus + jade, nothing gets compiled快递+手写笔+玉,什么都没有编译
【发布时间】:2013-10-05 12:54:05
【问题描述】:

我无法让这个简单的 app.js 工作:提供静态文件,但不编译jade 和 styl 文件。 这里是 __dirname ls:

damianomacbook:www damiano$ ls
app.jade    app.js      app.styl    config.xml  johnd.jpg

.jade 和 .styl 文件提供正常且简单的文件。 卷曲 css 和 html 文件时会发生什么(中间件函数应该即时生成):

damianomacbook:www damiano$ curl localhost:8080/app.css
curl: (52) Empty reply from server
damianomacbook:www damiano$ curl localhost:8080/app.html
Cannot GET /app.html

缺少什么?

有罪代码:

var express = require('express');
var stylus = require('stylus');
var nib = require('nib');

var app = express();
function compile(str, path) {
   return 
      stylus(str)
      .set('filename', path)
      .use(nib());
}

app.use(express.logger('dev'));

app.set('views', __dirname);
app.set('view engine', 'jade');

app.use(stylus.middleware({ 
    src: __dirname,
    compile: compile
}));

app.use(express.static(__dirname));

app.listen(8080);

【问题讨论】:

    标签: node.js express pug stylus


    【解决方案1】:

    您的 GET /app.html 失败,因为提供 HTML 页面是使用 express 路由器完成的,而不是中间件,并且您没有定义任何路由。静态中间件不转换任何东西(因此得名),因此除非磁盘上有实际的 app.html 文件,否则它不会为 /app.html 提供服务。要让/app.html 工作,请添加:

    app.get('/app.html', function (req, res) { res.render('app');});
    //or you probably want app.get('/', ...if you want this to be your home page
    //you probably also don't want/need ".html" in your URLs as this has fallen out of style
    

    您的手写笔问题是automatic semicolon insertion 怪物。您不能将“return”关键字单独放在一行上。您的 compile 函数返回 undefined 而不是手写笔实例。保持 compile 的格式与 nib 文档中的格式相同,一切都很好。

    【讨论】:

    • 谢谢彼得,完美。
    猜你喜欢
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多