问题描述
我无法使这个简单的app.js正常工作:提供了静态文件,但没有编译jade和styl文件.这里的__dirname ls:
I cannot get this simple app.js to work: static files are served but jade and styl files are not compiled.Here the __dirname ls:
damianomacbook:www damiano$ ls
app.jade app.js app.styl config.xml johnd.jpg
.jade和.styl文件可以正常使用.这里是卷曲css和html文件(中间件函数应该动态生成)时发生的事情:
.jade and .styl files are served normally and plain.Here what happens when curling css and html files (which the middlewares functions are supposed to generate on the fly):
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页面的操作是通过快速路由器而不是中间件完成的,并且您没有定义任何路由.静态中间件不会转换任何东西(因此没有名称),因此除非磁盘上有实际的app.html
文件,否则它不会提供/app.html
的服务.要使/app.html
工作,请添加:
Your GET /app.html is failing because serving HTML pages is done with the express router, not middleware, and you don't have any routes defined. The static middleware doesn't convert anything (thus the name), so it's not going to serve /app.html
unless there's an actual app.html
file on disk. To get /app.html
working, add:
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
您的手写笔问题是自动分号插入怪物.您一定不能将"return"关键字单独放在一行上.您的compile
函数将返回undefined
而不是手写笔实例.保持compile
格式不变,如nib文档中所述,一切都很好.
Your stylus problem is the automatic semicolon insertion monster. You must not put the "return" keyword on a line by itself. Your compile
function is returning undefined
instead of a stylus instance. Keep the compile
formatted as it is on the nib documentation and all is well.
这篇关于表达+手写笔+玉,什么都不会编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!