【发布时间】:2017-04-06 16:50:55
【问题描述】:
这似乎是 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'));
在我的 /views 中,我有名为 /characters 和 /series 的子文件夹 所以它看起来像 /views/characters/sample.ejs
现在在我的路线中,我有这个 ('ejs' 已在 app.js 中声明为视图引擎),
app.get('/series' , function(req,res){
res.render('series/some');
});
问题:在我将 some.ejs 文件移动到 /series 之前,
res.render('some');
完美运行。但是在将文件移动到 /series 后,我意识到 Node JS 无法呈现(嗯..找不到 some.ejs文件)。我可以删除所有子文件夹,然后将所有 .ejs 文件放在 /views 下,但这会很混乱。
附言。我尝试在 **app.js 尝试下传递多个目录。**
例如:
app.set('views', [path.join(__dirname, '/views'),path.join(__dirname,
'/views/characters/'), path.join(__dirname, '/views/series/')]);
文件结构:
/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
这个结构。所以我想渲染 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>
【问题讨论】: