简体   繁体   English

错误:无法查看视图->简单代码

[英]Error: Failed to lookup view -> simple code

I want compile a jade file with dynamic content. 我想用动态内容编译一个玉文件。 My code is from here http://jade-lang.com/tutorial/ : 我的代码来自http://jade-lang.com/tutorial/

app.get('/test', function(request, response){

var fn = jade.compileFile('views/test.jade');
var htmlOutput = fn({
  maintainer: {
    name: 'Forbes Lindesay',
    twitter: '@ForbesLindesay',
    blog: 'forbeslindesay.co.uk'
  }
});
response.render(htmlOutput);
});

I get the error below: 我收到以下错误:

Error: Failed to lookup view "<!DOCTYPE html><html lang="en"><h1></h1>Maintainer: Forbes L
        indesay</html><table><tr><td>Twitter</td><td>@ForbesLindesay</td></tr><tr><td>Blog</td><td
        >forbeslindesay.co.uk</td></tr></table>" in views directory "D:\test.js\views" "<!DOCTYPE html><html lang="en"><h1></h1>Maintainer: Forbes L
        indesay</html><table><tr><td>Twitter</td><td>@ForbesLindesay</td></tr><tr><td>Blog</td><td
        >forbeslindesay.co.uk</td></tr></table>" in views directory "D:\test.js\views"

It seems to be that the variables are correct (taken from the route.js). 似乎变量是正确的(取自route.js)。 What does the error mean? 错误是什么意思? There are a few questions like mine, but I cannot adapt it to my scenario... 有一些像我这样的问题,但我无法适应我的情况...

thanks in advance 提前致谢

You're trying to mix the standalone method of rendering Jade templates with the ability of Express to render them for you. 您正在尝试将呈现Jade模板的独立方法与Express的功能混合在一起。

Try this: 尝试这个:

app.get('/test', function(request, response) {
  response.render('test.jade', {
    maintainer: {
      name: 'Forbes Lindesay',
      twitter: '@ForbesLindesay',
      blog: 'forbeslindesay.co.uk'
    }
  });
});

Make sure Express is configured properly, though. 但是,请确保Express配置正确。 In your Express setup, use this: 在您的Express设置中,使用以下命令:

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

This assumes that the views/ directory is a direct child of the .js file where you're calling response.render() from. 假设views/目录是您从中调用response.render()的.js文件的直接子级。 More information here . 更多信息在这里

If you really want to use jade.compileFile() instead, you can use the suggestion in the comment made by @ArtemBaranovskii and use response.send(htmlOutput) . 如果确实要使用jade.compileFile() ,则可以在@ArtemBaranovskii的注释中使用建议,并使用response.send(htmlOutput)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM