NodeJS + Express + Handlebars - 找不到视图“index.html” [英] NodeJS + Express + Handlebars - failed to locate view "index.html"
问题描述
我在同一目录中有index.html作为app.js,所以我认为下面的代码没有问题定位index.html ...
我已经搜索,但它似乎是全面的除了玉石以外的任何事情都是罕见的...任何人都有这个组合的经验?
提前感谢!
var express = require('express')
,app = express.createServer();
app.configure(function(){
app.set('view engine','handlebars');
app.set(view options,{layout: false})
});
app.get('/',function(req,res){
var data = {
name:福特省,
家:Betelgeuse附近的一个小行星
}
res.render('index.html',data);
});
app.listen(3000);
更新:
我错过了:
app.set('views',__dirname +'/');
app.register('。html',require('handlebars'));
在我的配置...似乎'.html'的注册是非常重要的它指定了与.html的句柄关联...
我希望这有助于某人...
因为我是一个这样的人,我不能回答我自己的问题7个小时,但是如果有人需要完整的工作示例,我可以在明天发布...
默认情况下,它将从脚本目录中查找名为视图
的文件夹。如果你使用不同的目录,你必须指定它。
app.set('views',__dirname +'/ views') ;
Express还应该告诉你更多关于它在哪里找到视图的信息,这应该可以帮助你了解正好在哪里。
I have been playing a bit with Node.js. I recently started toying with Express and have been setting up a basic app. I wanted to use Handlebars as my view templating engine, but am hitting a wall - failed to locate view "index.html"
I have index.html in the same directory as app.js and and so I would think the code below would have no problem locating index.html...
I have searched around, but it would seem that comprehensive examples of anything aside from jade are rare... Anyone have experience with this combo?
Thanks in advance!
var express = require('express')
, app = express.createServer();
app.configure(function(){
app.set('view engine', 'handlebars');
app.set("view options", { layout: false })
});
app.get('/', function(req, res){
var data = {
name: "Ford Prefect",
home: "a small planet somewhere in the vicinity of Betelgeuse"
}
res.render('index.html', data);
});
app.listen(3000);
Update:
I was missing:
app.set('views', __dirname + '/');
app.register('.html', require('handlebars'));
in my config... it would seem that the register of '.html' is quite important as it specifies the handlebars association with .html...
I hope this helps someone...
Because I am a SO noob, I can't answer my own question for 7 hrs, but if anyone needs the complete working example, I can post tomorrow...
By default, it will look in a folder called views
from the directory the script is. If you use a different dir you must specify it.
app.set('views', __dirname + '/views');
Express should also tell you more information about where it's trying to find the view, that should help you know exactly where it's looking.
这篇关于NodeJS + Express + Handlebars - 找不到视图“index.html”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!