各位晚上好,
我将不胜感激任何帮助。
我得到了这两条路线:
app.get('/', function(req,res){
res.render('index');
});
app.get('/registration/add', function(req,res){
res.render('clientRegistration');
});
Run Code Online (Sandbox Code Playgroud)
这是服务器的一部分:
app.use( express.static(path.join( __dirname, './client/static/')));
app.use(body_parser.urlencoded());
app.set( "views", path.join(__dirname, "./client/views/"));
app.set( "view engine", 'ejs');
Run Code Online (Sandbox Code Playgroud)
目录是:
MyApp/
client/
static/
registration.css
styles.css
views/
index.ejs
clientRegistration.ejs
Run Code Online (Sandbox Code Playgroud)
在我的 index.ejs 上,我有: link rel="stylesheet" type="text/css" href="styles.css" 可以完美加载 .css 文件。但是,当我单击index.ejs上的链接:a href="/registration/add">时,它会将我带到clientRegistration.ejs页面,但即使我有:link rel="stylesheet",但registration.css也不会加载类型=“文本/css”href=“registration.css”
我究竟做错了什么?
谢谢你的时间。
我正在尝试在我的 Express 应用程序中使用此谷歌地图代码,但它给了我一个语法错误。我已经过滤了这个函数的错误:
<% var map; %>
<% function initMap() { %>
<% map = new google.maps.Map(document.getElementById('map'), { %>
<% center: {lat: -34.397, lng: 150.644}, %>
<% zoom: 8 %>
<% }); %>
<% }; %>
Run Code Online (Sandbox Code Playgroud)
错误消息并没有多大帮助:
SyntaxError: Unexpected token ; in C:\proyectos\google-location-api\views\resultados.ejs while compiling ejs
If the above error is not helpful, you may want to try EJS-Lint:
https://github.com/RyanZim/EJS-Lint
at Object.compile (C:\proyectos\google-location-api\node_modules\ejs\lib\ejs.js:524:12)
at Object.compile (C:\proyectos\google-location-api\node_modules\ejs\lib\ejs.js:338:16)
at handleCache (C:\proyectos\google-location-api\node_modules\ejs\lib\ejs.js:181:18)
at tryHandleCache (C:\proyectos\google-location-api\node_modules\ejs\lib\ejs.js:203:14)
at View.exports.renderFile [as engine] (C:\proyectos\google-location-api\node_modules\ejs\lib\ejs.js:412:10)
at View.render (C:\proyectos\google-location-api\node_modules\express\lib\view.js:128:8) …Run Code Online (Sandbox Code Playgroud) 我根据用户的角色创建了一个动态菜单栏。在服务器端,当用户登录时,我将他们所有允许的页面存储在他们的会话中。
现在,我有 15 个模板。menubar.html我在每一页上都包含了文件。我想使用会话数据,而menubar.html不必每次都显式传递它。
我尝试过以下代码:
app.get('/home',function(req,res)){
res.render('home',{menu : req.session.menubar})
}
app.get('/contact',function(req,res)){
res.render('contact',{menu : req.session.menubar})
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我需要在每个页面上传递会话数据。如果我有 50 个 html 模板,那么在每个路由中传递会话的效率并不高。
处理这个问题的最佳方法是什么?
我正在循环遍历 MySQL 表中的记录,将它们显示在 EJS 文件中,然后单击其中一个,路由到页面以编辑记录。但是,我无法将单个对象传递回我的routes.js 文件以将其路由到编辑页面。我循环浏览所有记录并打印它们没有任何问题,所以我知道数据就在那里。这是 EJS 文件的一部分,我在其中输出所有记录以供用户选择:
<body>
<div class="container">
<div class="col-sm-6 col-sm-offset-3">
<h1><span class="fa fa-chess"></span> View Roommate Agreements</h1><br>
<p align="center">Click on a Roommate Agreement below to view the full response and edit if necessary.</p><br>
<% for (var i=0; i < Agreements.length; i++) { %>
<a href="/editAgreement" style="text-decoration: none; color: #333333">
<div class="well" align="center">
<h3>Roommate Agreement for Room #<%= Agreements[i].roomNumber%></h3>
<p><%= Agreements[i].roommate1%></p>
<p><%= Agreements[i].roommate2%></p>
<% var Agreement = Agreements[i]%> //THIS is where I'm trying to declare the object
</div> …Run Code Online (Sandbox Code Playgroud) 下面是我正在渲染的 EJS 文件中的代码,但我无法找出 if 语句简写的正确语法。
这里的“cchoice”是我在渲染时传递的对象,如果我删除 if 语句它就可以工作,因此获取它的值没有问题。
<h1>Welcome,
<%= cchoice %>
</h1>
<% cchoice =='dog' ? <p>good choice</p> : <p>cool!</p> %>
Run Code Online (Sandbox Code Playgroud)
我收到的错误是:
SyntaxError: 编译 ejs 时 /views/new.ejs 中出现意外的标记 <
为上述 if 语句的简写提出正确的语法建议。
我目前正在尝试使用 ejs 为导航栏开发部分模板文件,但是我正在努力向已添加类的元素添加其他类。
我尝试了两种添加附加类的方法,这是第一种:
<li class='navbar-item'<% if (pageTitle === 'Home') { %> class="active" <% } %>> <a href="/home">Home</a></li>
Run Code Online (Sandbox Code Playgroud)
然而这不起作用,所以我决定尝试将 EJS 嵌入到类属性中,如下所示
<li class="nav-item <% if (pageTitle === 'Home') { %> active <% } %>">
Run Code Online (Sandbox Code Playgroud)
这也不起作用,我确保 pageTitle 被正确提供,因为它在网页中的其他地方使用并且工作正常。
我不确定下一阶段是什么,我考虑过使用额外的 JS 文件,但这似乎适得其反,因为我已经能够将 JS 嵌入到 HTML 中。
感谢任何帮助,提前致谢。
我正在制作一个简单的待办事项列表来学习 Node.js,并使用 EJS 进行模板化。我正在使用 for 循环来循环访问我设置的数组,以显示列表中的每个项目,并添加一个表单供用户添加到列表中。我的问题是,现在我已经创建了这个数组,但我尝试渲染它的地方什么也没有出现。显示数组时是否缺少某些内容?
编辑:我应该解释一下,我的特定代码(对于一个类)专门使用 for 循环而不是 forEach() 方法。我的问题也应该反映出我看不到我的代码和 stackoverflow 上其他一些代码之间的区别,因为这是一个拼写错误。
列表.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>To Do List</title>
</head>
<body>
<h1><%= whichDay %></h1>
<ul>
<% for (var i = 0; i < newListItems; i++) { %>
<li> <%= newListItems[i] %> </li>
<% } %>
</ul>
<form class="new-item" action="/" method="POST">
<input type="text" class="" name="newItem" placeholder="Add to list">
<button type="submit" name="button">Add</button>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
应用程序.js
const bodyParser = require(`body-parser`); …Run Code Online (Sandbox Code Playgroud) 我正在 YouTube 上关注 The Net Nijna 的教程。我到达了教程 27,在 ejs 中使用部分。一切正常,直到我添加 <% includepartials/nav.js %>,一旦我添加此代码,我就会收到:
SyntaxError:编译 ejs 时, (文件位置) testapp\views\profile.ejs中出现意外标识符
如果上述错误没有帮助,您可能想尝试 EJS-Lint:
https://github.com/RyanZim/EJS-Lint
或者,如果您打算创建异步函数,请async: true作为选项传递。在新功能()......等等等等......
如果我删除它,我的 ejs 一切正常。
<body>
<% include partials/nav.ejs %>
<h1>Welcome to the profile of <%= person %> !</h1>
<p><strong> Age: <%= data.age %></strong></p>
<p><strong> Job: <%= data.job %></strong></p>
<p><strong> Pet: <%= data.pet %></strong></p>
<h2>Hobbies</h2>
<ul>
<% data.hobbies.forEach(function(item){ %>
<li><%= item %></li>
<%});%>
</ul>
</body>
Run Code Online (Sandbox Code Playgroud)
你能帮助学生吗?万分感谢!
我正在使用Express、EJS和 MongoDB开发一个博客应用程序(单击链接查看GitHub存储库)。
我正在尝试引入添加帖子图像功能。作为 Express 的新手,我对遇到的问题感到困惑。
添加帖子表单:
<form action="/dashboard/post/add" method="POST" enctype="multipart/form-data" class="mb-0">
<div class="form-group">
<input type="text" class="form-control" name="title" value="<%= typeof form!='undefined' ? form.titleholder : '' %>" placeholder="Title" />
</div>
<div class="form-group">
<input type="text" class="form-control" name="excerpt" value="<%= typeof form!='undefined' ? form.excerptholder : '' %>" placeholder="Excerpt" />
</div>
<div class="form-group">
<textarea rows="5" class="form-control" name="body" placeholder="Full text"><%= typeof form!='undefined' ? form.bodyholder : '' %></textarea>
</div>
<label for="postimage">Upload an image</label>
<div class="form-group">
<input type="file" name="postimage" id="postimage" …Run Code Online (Sandbox Code Playgroud) ejs ×10
node.js ×9
express ×6
javascript ×5
css ×3
html ×2
arrays ×1
if-statement ×1
mysql ×1
node-modules ×1