为什么Node JS无法识别EJS的结尾括号

时间:2019-01-03 16:01:35

标签: node.js backend

我正在使用express和ejs在节点js中创建图片库 以下是我的app.js的代码

var express = require('express');
var app = express();
app.set("view engine", "ejs");
app.get("/", function(req, res){
res.render("welcome");
});

app.get("/campground", function(req, res){

var campground = [
{name : "Salmon Creek", image : 
"http://www.onguma.com/uploads/1/1/7/5/117537555/201604-aoba- 
 935_2_orig.jpg"},

 {name : "Granite Hill", image : "https://d2y0su6ixv655t.cloudfront.net/wp- 
 content/uploads/2014/07/16115316/BR15102803V_069.jpg"},

 {name : "Mountain Goat's Rest", image : 

https://www.colorado.com/sites/default/files/styles/1000x685/public/TroutLakeCamping_DC2.jpg?itok=2UO32aZ_”}    ];

res.render("campground", {campground:campground});
});

app.listen(5000, function(){
console.log("Server has been started");
 })

现在是露营地,以下是ejs文件       

这是露营地页面

  <% campground.forEach(function(camp){ %>
 <div>
<h4><%= camp.name %></h4>
<img src="<%= camp.image %>">
</div>
 <% >})%>
 <a href="/">Home Page</a> 

节点让%>混淆了上一行中图像标签中的img标签括号

并产生以下错误

  SyntaxError: Unexpected token > in 
 /projects/Backendt/YelpCamp/views/campground.ejs while compiling ejs

 If the above error is not helpful, you may want to try EJS-Lint:
 https://github.com/RyanZim/EJS-Lint
 Or, if you meant to create an async function, pass async: true as an 
 option.
 at new Function (<anonymous>)
 at Template.compile 
 (/projects/Backendt/YelpCamp/node_modules/ejs/lib/ejs.js:618:12)
 at Object.compile 
 (/projects/Backendt/YelpCamp/node_modules/ejs/lib/ejs.js:389:16)
 at handleCache 
 (/projects/Backendt/YelpCamp/node_modules/ejs/lib/ejs.js:212:18)
 at tryHandleCache 
 (/projects/Backendt/YelpCamp/node_modules/ejs/lib/ejs.js:251:16)
 at View.exports.renderFile [as engine] 
 (/projects/Backendt/YelpCamp/node_modules/ejs/lib/ejs.js:482:10)
at View.render 
 (/projects/Backendt/YelpCamp/node_modules/express/lib/view.js:135:8)
 at tryRender 
(/projects/Backendt/YelpCamp/node_modules/express/lib/application.js:640:10)
at Function.render 
 (/projects/Backendt/YelpCamp/node_modules/express/lib/application.js:592:3)
at ServerResponse.render 
(/projects/Backendt/YelpCamp/node_modules/express/lib/response.js:1008:7)

**请告诉我上述情况下如何使用ejs代码插入图片

1 个答案:

答案 0 :(得分:1)

此:

<% >})%>

应该是这样

<% })%>