Why node js is not recognizing ending brackets for ejs

心已入冬 提交于 2020-01-30 13:15:07

问题


i am creating an image gallery in node js using express and ejs The following is the code for my 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");
 })

Now for campground the following is the ejs file

This is the campground page

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

Node is confusing %> for img tag brackets in image tag in above line

and following error is generated

  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)

**Plz tell me how to insert image using ejs code for above case


回答1:


This:

<% >})%>

Should be this:

<% })%>


来源:https://stackoverflow.com/questions/54025818/why-node-js-is-not-recognizing-ending-brackets-for-ejs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!