首页 > 解决方案 > 服务器打开 SyntaxError: Unexpected token = in /home/michael/DemoApp/views/posts.ejs while compile ejs

问题描述

服务器已打开

SyntaxError: Unexpected token = in /home/michael/DemoApp/views/posts.ejs 同时编译 ejs

const express = require("express");
const app = express();
const port = process.env.PORT || 4000

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


app.get("/posts", function(req, res){
var posts = [
{title: "Once upon a time", author: "Richard"},
{title: "The Way", author: "Ikoro"},
{title: "I Love Critics", author: "Emmanuel"},
{title: "Adorable Bunny", author: "Christian"}
]
res.render("posts.ejs", {posts: posts});
});

app.listen(port, function(){
console.log("Server is open");
});

// The Codes for posts.ejs

<% for(var = i; i < posts.length; i++){ %>
<li>
    <%= posts[i].title %> - <strong><%= posts[i].author
    %></strong>
</li>
<% } %>

服务器已打开

SyntaxError: Unexpected token = in /home/michael/DemoApp/views/posts.ejs 同时编译 ejs

标签: node.jsejs

解决方案


在您的代码中var = i- 这是错误。它应该是:

<% for(var i = 0; i < posts.length; i++){ %>
<li>
    <%= posts[i].title %> - <strong><%= posts[i].author
    %></strong>
</li>
<% } %>

推荐阅读