簡體   English   中英

我正在嘗試執行一個簡單的 EJS 程序,但在編譯 EJS 時出現意外令牌 ')' 錯誤

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

我將在這里展示我的代碼:這是一個非常簡單的代碼,只顯示工作日或周末。 從 NPM 安裝程序安裝 EJS 包

這是 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>

這是 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"); })

這是 index.html 文件

這是我得到的錯誤:

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)
   

在此處輸入圖片說明

任何幫助將不勝感激:謝謝

標簽的正確語法是 - <%=並且應該以%>結尾。 您在兩邊都添加了= 請從結束標記中刪除。 應該是這樣的——

<h1>It is a <%= kindOfDay %> !</h1>

此外,您添加評論的方式在 Ejs 中也不正確。

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

這條評論正在執行。 請閱讀上中EJS添加評論的問題的答案。 正如鏈接問題的答案中提到的,您應該使用<%# comment %>作為評論。

最后,如果您嘗試在 Ejs 中包含主app.js文件,請不要這樣做。 Ejs 都是關於前端部分的。 因此,您應該包含在瀏覽器中運行的腳本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM