无法在nodejs中呈现Handlebars视图

时间:2016-04-12 02:56:25

标签: node.js express handlebars.js

所以我一直在尝试按照一些指南在线调试和修复我的情况,但它一直没有加载任何东西。所以我在这里问你们这些代码是否有问题:

var express = require('express');
var app = express();
var exphbs = require('express-handlebars');
var request = require('request');
var async = require('async');
var apiOptions = {
  useRedis: false,
  hostname: '127.0.0.1',
  port: 6379,
  cacheTTL: 7200
};
var lolapi = require('lolapi')('4df431ee-5631-4db3-b7d5-771c8aaf02f0', 'oce', apiOptions);
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');

app.get('/', function(req, res) {
  res.render('index', { title: 'Welcome to Api' });
});

1 个答案:

答案 0 :(得分:0)

根据documentation 你需要有结构

.
├── app.js
└── views
    ├── home.handlebars
    └── layouts
        └── main.handlebars

然后设置 views / layouts / main.handlebars

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Example App</title>
</head>
<body>

    {{{body}}}

</body>
</html>

并在 views / index.handlebars

中编制index.handlebars
<h1>App Title: {{{title}}}</h1>