简体   繁体   English

渲染把手页面 Node.js

[英]Rendering handlebars Page Node.js

I am attempting to render a handlebars page and I am getting my 404 error and I am not sure why.我正在尝试呈现一个把手页面,但我收到了 404 错误,我不知道为什么。 It's almost as if it does not recognize the handlebars file.就好像它无法识别把手文件一样。 My file hierarchy looks like this:我的文件层次结构如下所示: 在此处输入图片说明

I am calling the path with this section of the current page:我正在使用当前页面的这一部分调用路径:

<p><a href="http://18.219.103.143:3000/admin-dashboard">Admin Login</a></p>

In the main js file, I am handling the admin-dashboard with this code:在主 js 文件中,我使用以下代码处理admin-dashboard

app.get('/admin-dashboard', function(req,res){
  var context = {};
  res.render('adminDash', context);
});

adminDash.handlebars only contains: adminDash.handlebars仅包含:

<h1>Admin Page</h1>

View Engine:查看引擎:

app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
app.set('port', 3000);
app.use(express.static('public'));

I tried console.log and/or alert in the app.get function to debug but nothing printed to console.我在app.get函数中尝试了 console.log 和/或 alert 进行调试,但没有打印到控制台。 Might it be the way I am calling the render() ?可能是我调用render()吗? Not sure how to debug when I can't print statements.当我无法打印语句时,不确定如何调试。 Does anything look obviously out of place?有什么看起来明显不合适的地方吗?

What view engine do you use and how have you set up it?您使用什么视图引擎以及如何设置它? If you use the default hbs then the suffix is .hbs如果你使用默认的hbs那么后缀是.hbs

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
  1. Install handlebars: npm i express-handlebars安装把手: npm i express-handlebars

  2. Add code:添加代码:

const exphbs = require('express-handlebars'); const exphbs = require('express-handlebars');

const hbs = exphbs.create({
    defaultLayout: 'main',
    extname: 'hbs',
    runtimeOptions: {
        allowProtoPropertiesByDefault: true,
        allowProtoMethodsByDefault: true,
    }
});
app.engine('hbs', hbs.engine);
app.set('view engine', 'hbs');
app.set('views', 'views');

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

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