Issue
I am going to show my code here: It is a very simple one to just display Either Weekday or Weekend.
Installed EJS package from NPM installer
This is list.ejs (inside the views folder)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>T Do List</title>
<script src="app.js"></script>
</head>
<body>
<!-- The EJS is represented by <%= EJS =%> -->
<h1>It is a <%= kindOfDay =%> !</h1> <!-- Corrected -->
</body>
</html>
This is the app.js
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.set('view engine','ejs'); //for EJS
app.get("/", function(req, res){
var today = new Date();
var currentDay = today.getDay();
var day = "";
if(currentDay === 6 || currentDay === 0) // Sunday - Saturday : 0:6
{
day = "weekend";
// list has to exist in views folder || EJS keyword has to match the {key: value} pair
}
else {
day = "weekday";
}
res.render("list", {kindOfDay: day});
});
app.listen(3000, function(){
console.log("Server running at port 3000");
})
This is the index.html file
And this is the error I am getting :
SyntaxError: Unexpected token ')' in ..\todoList-v1\views\list.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 (..\todoList-v1\node_modules\ejs\lib\ejs.js:662:12)
Any help would be appreciated: Thank you
Solution
The correct syntax for the tag is – <%=
and should be ends with %>
. You have added =
on both sides. Please remove from the ending tag. It should be like –
<h1>It is a <%= kindOfDay %> !</h1>
Also, the way you added comment is not correct in Ejs.
<!-- The EJS is represented by <%= EJS =%> -->
This comment is executing. Please read this question’s answer on adding comment within Ejs. As mentioned in the linked question’s answer, you should use <%# comment %>
for the comment.
Finally, if you’re trying to include the main app.js
file in Ejs, then don’t. Ejs all about front-end side part. So, you should include scripts that runs in browser.
Answered By – Viral Patel
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0