简体   繁体   English

我正在尝试执行一个简单的 EJS 程序,但在编译 EJS 时出现意外令牌 ')' 错误

[英]I am Trying to execute a simple EJS program, But getting Unexpected Token ')' error while compiling EJS

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从 NPM 安装程序安装 EJS 包

This is list.ejs (inside the views folder)这是 list.ejs(在 views 文件夹内)

    <!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这是 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这是 index.html 文件

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任何帮助将不胜感激:谢谢

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.此外,您添加评论的方式在 Ejs 中也不正确。

<!-- The EJS is represented by <%= EJS =%> -->

This comment is executing.这条评论正在执行。 Please read this question's answer on adding comment within Ejs.请阅读上中EJS添加评论的问题的答案。 As mentioned in the linked question's answer, you should use <%# comment %> for the comment.正如链接问题的答案中提到的,您应该使用<%# comment %>作为评论。

Finally, if you're trying to include the main app.js file in Ejs, then don't.最后,如果您尝试在 Ejs 中包含主app.js文件,请不要这样做。 Ejs all about front-end side part. Ejs 都是关于前端部分的。 So, you should include scripts that runs in browser.因此,您应该包含在浏览器中运行的脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM