发布于2023-05-29 20:59 阅读(1169) 评论(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/
来源:前端黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 前端黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-3
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!