如何使用nodejs,expressjs渲染jade文件

时间:2012-11-20 15:26:36

标签: node.js pug express

我使用快递工具“express -s”创建了一个项目, 默认情况下,它将jade指定为默认视图模板系统。

我还创建了一个文件index.html并使用html2jade工具转换它,问题是当我尝试使用此函数访问index.jade文件时:

exports.index = function(req, res){
     console.log('welcome admin');
     res.render('admin/app/index', {  }); 
}

我收到了这个错误

SyntaxError: Unexpected token .
at Object.Function (unknown source)

请有人解释一下是什么问题:)

提前致谢。

3 个答案:

答案 0 :(得分:1)

That problem seems to be some formatting issue. you can use ejs instead. add the following to your app.js:
// map .renderFile to ".html" files
app.engine('html', require('ejs').renderFile);

// make ".html" the default
app.set('view engine', 'html');

//Then you can use below code to render html files and code in ejs:
res.render('index.html);

答案 1 :(得分:0)

可能是您没有向索引文件发送任何数据。我会尝试这样的事情......

    exports.index = function(req, res){
      console.log('welcome admin');
      res.render('index', { output: 'Welcome Admin' }); 
    }

然后在你的app.js文件中想要

    app.get('/', routes.index);

然后在index.jade文件中应该有输出key.so的标记,无论输出键的值是什么,都会打印在模板中。

答案 2 :(得分:0)

该错误通常意味着模板中的Jade语法存在问题,因此我会仔细检查 - 终端的输出应包含有关解析错误发生位置的更详细信息。

此外,如果您尚未配置它,我建议添加以下内容以启用Connect的错误处理程序:

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); 
});
相关问题