Express忽略views目录 [英] Express ignoring views directory

查看:52
本文介绍了Express忽略views目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个配置文件来存储我的快递应用程序的应用程序路径,cookie秘密等设置。问题是它似乎忽略了我的视图路径目录设置。

I have set up a config file to store settings like application path, cookie secret and the like for my express app. The problem is it seems to be ignoring my view path directory setting.

config.js:

config.js:

...
exports.server = {
    port: 3000,
    cookie_secret: ".....",
    path: "/var/www/onmynode-dev/"
}
...

app .js:

...
app.set('views', path.join(config.server.path, 'views'));
app.set('view engine', 'html');
app.engine('html', require('express3-handlebars')({defaultLayout: "default.html"}));
...

路线设置如下:

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

视图从请求中调用如下:

The view is called from the request as follows:

exports.index = function(req, res){
    res.render('index');
};

app.log的app对象(var app = express();)在我的最后app.js文件。

console.log of app object (var app = express();) at the very end of my app.js file.

...
settings: 
{ 'x-powered-by': true,
 etag: true,
 env: 'development',
 'subdomain offset': 2,
 view: [Function: View],
 views: '/var/www/onmynode-dev/views',
 'jsonp callback name': 'callback',
 'json spaces': 2,
 port: 3000,
 'view engine': 'html' 
},
...

因此看起来视图设置正确但在运行应用程序并加载页面时我们得到以下内容:

So it looks like the view is being set correctly but on running the app and loading a page we get the following:

500 Error: ENOENT, open '/home/user/views/layouts/default.html'

所以看来无论我如何设置,都要使用__dirname变量。问题是如何调试/修复此问题?

So it appears to be using the __dirname variable regardless of how I have set things up. Question is how do I debug/fix this issue?

推荐答案

在黑暗中拍摄,但我只是阅读 express3-handlebars docs。

Shot in the dark, but I just read the express3-handlebars docs.


布局



布局只是一个Handlebars模板,其中包含 {{{body}}}
占位符。通常它将是一个HTML页面包装器,其中将呈现视图

Layouts

A layout is simply a Handlebars template with a {{{body}}} placeholder. Usually it will be an HTML page wrapper into which views will be rendered.

此视图引擎添加了layout的概念,该概念已被删除$快递3.x中的b $ b。它可以配置布局
目录的路径,默认设置为views / layouts /\".

This view engine adds back the concept of "layout", which was removed in Express 3.x. It can be configured with a path to the layouts directory, by default it's set to "views/layouts/".

有两种方法可以设置默认布局:配置视图
引擎的 defaultLayout 属性,或设置Express locals
app.locals.layout。

There are two ways to set a default layout: configuring the view engine's defaultLayout property, or setting Express locals app.locals.layout.

通过为布局请求
local分配不同的值,可以为每个请求重写
每个请求的布局。以下将呈现没有布局的主页视图:

The layout into which a view should be rendered can be overridden per-request by assigning a different value to the layout request local. The following will render the "home" view with no layout:

app.get('/', function (req, res, next) {
    res.render('home', {layout: false}); });


或许它指的是 __ dirname +'/ views'并忽略您在配置中设置的内容。

Perhaps it refers to __dirname+'/views' and ignores what you set in the config.

尝试添加 {layout:false} 像上面的代码一样。如果它有效,那么这就是你的问题。

Try adding {layout: false} like the above code. If it works, then that is your issue.

持续阅读让我发现你可以改变把手寻找布局的位置。您可以像在 defaultLayout 中一样将 layoutsDir 添加到配置中,并将其设置为与Express视图相同的目录:

Continual reading led me to find that you can change where handlebars looks for its layouts. You can add a layoutsDir to the configuration like you did with defaultLayout and set it to the same directory as your Express views:

var hbConfig = {
    layoutsDir: path.join(app.settings.views, "layouts"), 
    defaultLayout: "default.html"
} 
app.engine('html', require('express3-handlebars')(hbConfig));

这篇关于Express忽略views目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆