SyntaxError: Unexpected token ';' in <file path> while compiling ejs

SyntaxError: Unexpected token ';' in <file path> while compiling ejs

每当我重新加载我的网站时,我都会收到此错误:

SyntaxError: Unexpected token ';' in /Users/<name>/Desktop/Web_Development/ejs-challenge/views/home.ejs while compiling ejs

这是我的 app.js:

//jshint esversion:6

const express = require("express");
const ejs = require("ejs");

const homeStartingContent = "<placeholder text>";
const contactContent = "<placeholder text>";
const aboutContent = "<placeholder text>";

const app = express();

let posts = [];

app.set('view engine', 'ejs');

app.use(express.json());

app.use(express.urlencoded({extended: true}));

app.use(express.static("public"));

app.get("/", function(req, res){
  res.render("home.ejs", {homeContent: homeStartingContent, 
  posts: posts});
});

app.get("/about", function(req, res){
  res.render("about.ejs", {aboutContent: aboutContent,});
});

app.get("/contact", function(req, res){
  res.render("contact.ejs", {contactContent: contactContent,});
});

app.get("/compose", function(req, res){
  res.render("compose.ejs");
});

app.post("/compose", function(req, res){

  const post = {
    title: req.body.postTitle,
    body: req.body.postBody,
  };

  posts.push(post);

  res.redirect("/");
});

app.listen(3000, function() {
  consol

e.log("服务器在 3000 端口启动"); });

这是我的 ejs:

<%- include('partials/header') -%>
<h1>Home</h1>
<p><%= homeContent %> </p>

<% posts.forEach(post => %>
    <h1> <%= post.title %> </h1>
    <p>  <%= post.body %> </p>
<% )) %>

<%- include('partials/footer') -%>

ejslint 告诉我:

Unexpected token (6:9) in home.ejs
<h1><%= post.title %></h1>
    ^    

我不知道如何修复它,我已经删除了所有的“;”那是在我的 home.ejs 文件中,我也尝试使用普通函数, Ejslint 至少对我来说不是很有帮助。

你有两个括号去掉一个

<% posts.forEach(post => %>
    <h1> <%= post.title %> </h1>
    <p>  <%= post.body %> </p>
<% )) %>

改成这样

<% posts.forEach(post => %>
        <h1> <%= post.title %> </h1>
        <p>  <%= post.body %> </p>
    <% ) %>