NodeJS 将ejs与express一起使用返回意外的标记

neekobn8  于 2023-01-30  发布在  Node.js
关注(0)|答案(3)|浏览(152)

this is my first try at using Ejs, i think i got all my syntax right. cant understand whats wrong

const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");

const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended:true}));
let today = new Date();
let options = {
  weekday: "long",
  year: "numeric",
  month: "long",
  day: "numeric"
};

let day = today.toLocaleDateString("en-US", options);
let todoList = ["Sleep","Eat","?"];
app.get("/", function(req, res){
 res.render("index", {
   day: day,
   todoList: todoList
 } );
});
app.post("/", function(req, res){
  let newInput = req.body.todoInput;
  todoList.push(newInput);
});

app.listen(3000, function(){
  console.log("Server started on port 3000.");
});

my ejs

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>Ra's To-do List</title>
</head>

<body>
  <p>
    <%= day %> : Today's to-do list</p>
  <ul>
     <% for (var i=0, i< todoList.length, i++) { %>
    <li> <%= todoList[i] %> </li>
    <% } %>
  </ul>
  <form action="/" method="post">
    <input type="text" name="todoInput" placeholder="Add to your to-do list here">
    <button type="submit" name="button">Add stuff to-do</button>
  </form>
</body>

</html>

Im using WSL and have installed all the modules properly. it gives the unexpected token < in .... while compiling ejs. ive been at this for a couple of hours now and i might break my laptop soon :)
SyntaxError: Unexpected token < in /mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/views/index.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 () at Template.compile (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/ejs/lib/ejs.js:618:12) at Object.compile (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/ejs/lib/ejs.js:389:16) at handleCache (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/ejs/lib/ejs.js:212:18) at tryHandleCache (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/ejs/lib/ejs.js:251:16) at View.exports.renderFile [as engine] (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/ejs/lib/ejs.js:482:10) at View.render (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/express/lib/view.js:135:8) at tryRender (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/express/lib/application.js:640:10) at Function.render (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/express/lib/application.js:592:3) at ServerResponse.render (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/express/lib/response.js:1008:7)

q3qa4bjr

q3qa4bjr1#

我认为问题在于你在ejs语句中使用逗号而不是分号。
因此,代替:

<% for (var i=0, i< todoList.length, i++) { %>

它应该是:

<% for (var i=0; i< todoList.length; i++) { %>

干杯艾登。

pgx2nnw8

pgx2nnw82#

问题是,“〈”小于符号。因为您不能在html上使用。您应该更改html实体,如“<“(删除一个空格字符)。并检查它谷歌另一个html实体符号。祝你好运。

z9zf31ra

z9zf31ra3#

我在我的node-ejs应用程序(SyntaxError: Unexpected token ; ...)中遇到了同样(类似)的错误。我认为这个错误的一般答案是仔细检查你的ejs代码。通常你的代码中有语法错误。对我来说,我注意到当我在代码中使用ejs变量时(例如<%= email %>email是一个变量),我使用了如下错误的语法:

<% email =%>

更正后,错误消失了。在我看来,这个错误中显示的文本是虚幻的。这个问题实际上与async function无关,通常token <token ;或...中提到的字符与真实的的语法错误无关。

相关问题