开发者

Expressjs app.render isn't working

开发者 https://www.devze.com 2023-02-25 19:11 出处:网络
When I call app.get(\'/\', function(req, res)开发者_开发百科 { res.render(\'index\', {locals: {title: \'Hello, Node!\' }});

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});
        }
    }
});


Also which version of express / node are you using? Try upgrading to the latest stable ones.

// render
var str = view.fn.call(options.scope, options);

That's what renders your view.

view.fn = engine.compile(view.contents, options)

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 in haml 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消