gpt4 book ai didi

node.js - express + stylus + jade,没有编译任何内容

转载 作者:太空宇宙 更新时间:2023-11-03 22:17:42 25 4
gpt4 key购买 nike

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

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

.jade 和 .styl 文件正常且简单地提供。这是 curl 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);

最佳答案

您的 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 文档中的格式一致,一切顺利。

关于node.js - express + stylus + jade,没有编译任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19198042/

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