【发布时间】: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 ` ` 而你正在做的
<%- kindOfDay %> -
是更正了正确的 EJS 格式。但是,尝试只使用 express 和 list.ejs 来运行它。不会工作是吗?关于替代品的任何想法
标签: javascript html express typeerror ejs