这似乎是用Node JS + Express来解决的一个非常简单的问题,我已经做了一些研究来解决我的问题,但是结果没有出来,所以我在这里发布问题!
我要做的就是从路由中渲染.ejs文件。就像任何其他基本Node JS应用程序的文件结构一样,我的应用程序分为
/public、/views、/routes/、node_modules、app.js、package.json
在我的app.js中,我已经声明
app.set('views', path.join(__dirname, '/views'));
app.use('/', express.static(__dirname + '/public'));
Run Code Online (Sandbox Code Playgroud)
在我的/ views内部,我有一个名为/ characters和/ series的子文件夹, 所以它看起来像/views/characters/sample.ejs
现在在我的路线中,我已经有了这个('ejs'在app.js中被声明为视图引擎),
app.get('/series' , function(req,res){
res.render('series/some');
});
Run Code Online (Sandbox Code Playgroud)
问题:在将some.ejs文件移到/ series之前,
res.render('some');
Run Code Online (Sandbox Code Playgroud)
工作正常。但是将文件移到/ series后,我意识到Node JS无法渲染(嗯..找不到some.ejs文件)。我可以删除所有子文件夹并删除所有.ejs / views下的文件,但是会很乱。
PS。我尝试在** app.js尝试下传递多个目录。**
例如:
app.set('views', [path.join(__dirname, '/views'),path.join(__dirname,
'/views/characters/'), path.join(__dirname, '/views/series/')]);
Run Code Online (Sandbox Code Playgroud)
档案结构:
/myapp
->/node_modules
->/public
->/css
->/fonts
->/images
->/js
->/template
->/video
->/routes
->characters.js
->etc.js
->index.js
->series.js
->/views
->/characters
->characters_index.ejs
->/series
->series_index.ejs
->index.ejs
->app.js
->package.json
Run Code Online (Sandbox Code Playgroud)
这个结构。所以我想渲染series_index.ejs或characters_index.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<title><% include ../public/template/title %></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<% include ../public/template/scripts %>
</head>
<body id="series_body">
<% include ../public/template/background_effect %>
<% include ../public/template/navigation_bar %>
<div class="container-fluid">
<div class="row content-fluid">
<div class="col-xs-4 col-sm-4 col-md-4 col-lg-4 " >
<div id="series_description" class="content-fluid">
<h2>Batman (1989)</h2>
<a href="/series/batman" ><img class="img-responsive"
src="http://localhost:3000/images/series/batman_1989.jpg" width="200px"
height="300px"/></a>
</div>
<div id="series_description" class="content-fluid">
<h2>Batman and Robin (1997)</h2>
<a href="/series/batman_and_robin"><img class="img-responsive"
src="http://localhost:3000/images/series/batman_and_robin.png"
width="200px" height="300px"/></a>
</div>
<div id="series_description" class="content-fluid">
<h2>The Dark Knight Rises (2012)</h2>
<a href="/series/the_dark_knight_rises"><img class="img-responsive"
src="http://localhost:3000/images/series/the_dark_knight_rises.jpg"
width="200px" height="300px"/></a>
</div>
</div>
<div class="col-xs-4 col-sm-4 col-md-4 col-lg-4 " >
<div id="series_description" class="content-fluid">
<h2>Batman Returns (1992)</h2>
<a href="/series/batman_returns"><img class="img-responsive"
src="http://localhost:3000/images/series/batman_returns.png"
width="200px" height="300px"/></a>
</div>
<div id="series_description" class="content-fluid">
<h2>Batman Begins (2005)</h2>
<a href="/series/batman_begins"><img class="img-responsive"
src="http://localhost:3000/images/series/batman_begins.jpg"
width="200px" height="300px"/></a>
</div>
</div>
<div class="col-xs-4 col-sm-4 col-md-4 col-lg-4 " >
<div id="series_description" class="content-fluid">
<h2>Batman Forever (1995)</h2>
<a href="/series/batman_forever"><img class="img-responsive"
src="http://localhost:3000/images/series/batman_forever.png"
width="200px" height="300px"/></a>
</div>
<div id="series_description" class="content-fluid">
<h2>The Dark Knight (2008)</h2>
<a href="/series/the_dark_knight"><img class="img-responsive"
src="http://localhost:3000/images/series/the_dark_knight.jpg"
width="200px" height="300px"/></a>
</div>
</div>
</div>
</div>
<% include ../public/template/footer %>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我认为问题出在您的斜杠上,请尝试设置以下数组的视图:
app.set('views', [path.join(__dirname, 'views'),
path.join(__dirname, 'views/characters/'),
path.join(__dirname, 'views/series/')]);
Run Code Online (Sandbox Code Playgroud)
然后只需正常调用您的视图即可:
res.render('some');
您可以通过以下方式检查已安装的视图文件夹:
app.get('views');
| 归档时间: |
|
| 查看次数: |
2953 次 |
| 最近记录: |