程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Using ejs with express returns unexpected token < used

发布于2023-05-29 20:59     阅读(1089)     评论(0)     点赞(18)     收藏(1)


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)


解决方案


I think the problem is just that you are using commas instead of semi-colons in your ejs statement.

So instead of:

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

It should be:

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

Cheers, Aidan.




所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.qianduanheidong.com/blog/article/528349/c17cb6e8ee378cfd5398/

来源:前端黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

18 0
收藏该文
已收藏

评论内容:(最多支持255个字符)