node.js 根据调用 res.render() 的文件的位置更改 Express 视图文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21885377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Express view folder based on where is the file that res.render() is called
提问by Talysson
I would like to change the view folder of Express when I call res.render().
我想在调用 res.render() 时更改 Express 的视图文件夹。
For example, if I call res.render(viewName) inside /folder/file.js, I would like that Express look for the view inside /folder/views.
例如,如果我在 /folder/file.js 中调用 res.render(viewName),我希望 Express 在 /folder/views 中查找视图。
If the file is inside /folder1/folder2/file.js, I would like that Express look for the view inside /folder1/folder2/views
如果文件在 /folder1/folder2/file.js 中,我希望 Express 在 /folder1/folder2/views 中查找视图
Is it possible ?
是否可以 ?
回答by Pedro Nasser
You can use the method set()to redefine express's default settings.
您可以使用该方法set()重新定义 express 的默认设置。
app.set('views', path.join(__dirname, '/yourViewDirectory'));
For a dynamic path change you can do something like this:
对于动态路径更改,您可以执行以下操作:
var express = require('express');
var path = require('path');
var app = express();
app.engine('jade', require('jade').__express);
app.set('view engine','jade');
app.customRender = function (root,name,fn) {
var engines = app.engines;
var cache = app.cache;
view = cache[root+'-'+name];
if (!view) {
view = new (app.get('view'))(name, {
defaultEngine: app.get('view engine'),
root: root,
engines: engines
});
if (!view.path) {
var err = new Error('Failed to lookup view "' + name + '" in views directory "' + root + '"');
err.view = view;
return fn(err);
}
cache[root+'-'+name] = view;
}
try {
view.render(opts, fn);
} catch (err) {
fn(err);
}
}
app.get('/', function(req, res) {
app.customRender(path.join(__dirname, '/path/to/user/'),'index',function (err,html) {
if (err)
res.send(404);
else
res.send(200,html);
});
});
app.listen(3000);
回答by nuzzolilo
Instead of simply passing your view name to the render function, you can pass a relative or? ?absolute?? ?path.??????
不是简单地将您的视图名称传递给渲染函数,您可以传递一个相对的或??绝对???小路。??????
Simple example:
简单的例子:
app.get('/your/path', function(req, res) {
//viewname can include or omit the filename extension
res.render(__dirname + '/folder/with/views/viewname');
});??????????
回答by Vickar
That's pretty simple
这很简单
to change the view folder of Express when one calls res.render(), just set the path where the views are located, in your case,
要在调用 res.render() 时更改 Express 的视图文件夹,只需设置视图所在的路径,在您的情况下,
app.set('views','./folder1/folder2/views');
This changes the path where Express would search for specified views.
这会更改 Express 搜索指定视图的路径。
回答by Luke Chinworth
You can also get the relative path by using require.resolve:
res.render(require.resolve('./folder/with/views/viewname'));
您还可以使用require.resolve以下方法获取相对路径:
res.render(require.resolve('./folder/with/views/viewname'));
回答by Collin
(Sorry I can't comment yet)
(抱歉我还不能评论)
@nuzzolilo's answerworks well. But if you prefer ES6
@nuzzolilo 的回答很有效。但如果你更喜欢 ES6
app.get('/path', function (req, res) {
res.render(`${__dirname}/templates_dir/index`, { data: "value" });
});
This simply improves code readability ;)
这只是提高了代码的可读性;)

