Expressjs app.render 不起作用
当我称之为
app.get('/', function(req, res)
{
res.render('index', {locals: {title: 'Hello, Node!' }});
});
输出
TypeError: Object "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" +
"<html>" +
"</html>" +
"<title>" +
title +
"</title>" +
"<body>" +
"</body>" +
"<s>" +
"Hello, World" +
"</s>" has no method 'call'
at ServerResponse._render (/usr/local/lib/node/.npm/express/2.2.2/package/lib/view.js:377:21)
at ServerResponse.render (/usr/local/lib/node/.npm/express/2.2.2/package/lib/view.js:242:17)
at Object.<anonymous> (/home/william/www/html_public/app.js:36:6)
at param (/usr/local/lib/node/.npm/connect/1.3.0/package/lib/middleware/router.js:148:21)
at pass (/usr/local/lib/node/.npm/connect/1.3.0/package/lib/middleware/router.js:164:10)
at Object.router [as handle] (/usr/local/lib/node/.npm/connect/1.3.0/package/lib/middleware/router.js:170:6)
at next (/usr/local/lib/node/.npm/connect/1.3.0/package/lib/http.js:204:15)
at Object.bodyParser [as handle] (/usr/local/lib/node/.npm/connect/1.3.0/package/lib/middleware/bodyParser.js:76:7)
at next (/usr/local/lib/node/.npm/connect/1.3.0/package/lib/http.js:204:15)
at Object.methodOverride [as handle] (/usr/local/lib/node/.npm/connect/1.3.0/package/lib/middleware/methodOverride.js:35:5)
时这是我的快速配置
app.configure(function(){
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(app.router);
app.use(express.cookieParser());
app.set('view engine', 'haml');
app.set("view options", { layout: false });
// Set directorys
app.use(express.static(public_dir));
app.set('views', __dirname + '/views');
});
我该如何解决这个问题。似乎调用了一个不存在的函数?
这是 haml 代码
!!!
%html
%title= title
%body
%s Hello, World
已修复!! 此配置修复了与 haml 库的兼容性
var haml = require('haml');
app.register('.haml', {
compile: function(str, options) {
return function(locals) {
return haml.render(str, {locals: locals});
}
}
});
When I call
app.get('/', function(req, res)
{
res.render('index', {locals: {title: 'Hello, Node!' }});
});
it outputs
TypeError: Object "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" +
"<html>" +
"</html>" +
"<title>" +
title +
"</title>" +
"<body>" +
"</body>" +
"<s>" +
"Hello, World" +
"</s>" has no method 'call'
at ServerResponse._render (/usr/local/lib/node/.npm/express/2.2.2/package/lib/view.js:377:21)
at ServerResponse.render (/usr/local/lib/node/.npm/express/2.2.2/package/lib/view.js:242:17)
at Object.<anonymous> (/home/william/www/html_public/app.js:36:6)
at param (/usr/local/lib/node/.npm/connect/1.3.0/package/lib/middleware/router.js:148:21)
at pass (/usr/local/lib/node/.npm/connect/1.3.0/package/lib/middleware/router.js:164:10)
at Object.router [as handle] (/usr/local/lib/node/.npm/connect/1.3.0/package/lib/middleware/router.js:170:6)
at next (/usr/local/lib/node/.npm/connect/1.3.0/package/lib/http.js:204:15)
at Object.bodyParser [as handle] (/usr/local/lib/node/.npm/connect/1.3.0/package/lib/middleware/bodyParser.js:76:7)
at next (/usr/local/lib/node/.npm/connect/1.3.0/package/lib/http.js:204:15)
at Object.methodOverride [as handle] (/usr/local/lib/node/.npm/connect/1.3.0/package/lib/middleware/methodOverride.js:35:5)
This is my express configuration
app.configure(function(){
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(app.router);
app.use(express.cookieParser());
app.set('view engine', 'haml');
app.set("view options", { layout: false });
// Set directorys
app.use(express.static(public_dir));
app.set('views', __dirname + '/views');
});
How do I fix this whats wrong. It seems to be calling a function which doesnt exists??
This is the haml code
!!!
%html
%title= title
%body
%s Hello, World
Fixed!!
This configure fixes the compatibly with the haml libary
var haml = require('haml');
app.register('.haml', {
compile: function(str, options) {
return function(locals) {
return haml.render(str, {locals: locals});
}
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
另外您使用的是哪个版本的express/node?尝试升级到最新的稳定版。
这就是你的观点。
这就是创建该函数的原因。
.compile
在您的 haml 引擎中定义。我猜这是 haml 引擎中的一个错误,因为那是一个非标准引擎。haml 不符合开箱即用的 Express 标准。所有快速视图引擎都应该有一个
.compile
函数,该函数返回一个可以调用以返回 html 的函数。haml
中的.compile
方法不会返回 express 期望的结果。尝试 haml-js 这是由 Express 人员编写并由 Express 人员维护的。
或者,如果您阅读 app.register 的文档,它会向您展示如何将任何视图引擎变成快速兼容的视图引擎。
Also which version of express / node are you using? Try upgrading to the latest stable ones.
That's what renders your view.
That's what creates the function.
.compile
is defined in your haml engine. My guess it's a bug in the haml engine as that's a non standard engine.haml is not compliant with express out of the box. All express view engines are expected to have a
.compile
function which returns a function that can be called to return html. the.compile
method inhaml
does not return what express expects.try haml-js which was written by the express guys and is maintained by the express guys.
Alternatively if you read the docs for app.register it shows you how to turn any view engine into an express compatible view engine.