【问题标题】:I am Trying to execute a simple EJS program, But getting Unexpected Token ')' error while compiling EJS我正在尝试执行一个简单的 EJS 程序,但在编译 EJS 时出现 Unexpected Token ')' 错误
【发布时间】:2021-09-16 00:27:26
【问题描述】:

我将在这里展示我的代码:仅显示 Weekday 或 Weekend 是一个非常简单的代码。 从 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)
   

任何帮助将不胜感激:谢谢

【问题讨论】:

  • 您为什么要尝试将 node express 脚本加载到浏览器网页中?那是行不通的。它们是两个完全不同的 JS 环境。
  • 这就是它必须是 ig ` ` 而你正在做的&lt;%- kindOfDay %&gt;
  • 是更正了正确的 EJS 格式。但是,尝试只使用 express 和 list.ejs 来运行它。不会工作是吗?关于替代品的任何想法

标签: javascript html express typeerror ejs


【解决方案1】:

tag 的正确语法是 - &lt;%=,应该以 %&gt; 结尾。您在两边都添加了=。请从结束标记中删除。应该是-

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

另外,您添加评论的方式在 Ejs 中不正确。

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

这条评论正在执行。请阅读this 在 Ejs 中添加评论的问题答案。如链接问题的答案中所述,您应该使用&lt;%# comment %&gt; 进行评论。

最后,如果您尝试在 Ejs 中包含主 app.js 文件,请不要这样做。 Ejs 都是关于前端部分的。因此,您应该包含在浏览器中运行的脚本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    • 2020-01-13
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 2020-11-05
    • 1970-01-01
    相关资源
    最近更新 更多