gpt4 book ai didi

javascript - nodejs/ejs 包含部分语法错误

转载 作者:行者123 更新时间:2023-12-01 00:23:13 25 4
gpt4 key购买 nike

我有一个非常奇怪的问题,我无法弄清楚

我在尝试加载网页时收到此错误。我试图非常简单地使用页眉和页脚的部分 View 。我尝试了各种方法,甚至开始一个新项目,执行 npm init、npm install express ejs

SyntaxError: missing ) after argument list in layout.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 (<anonymous>)
at Template.compile (\node_modules\ejs\lib\ejs.js:626:12)
at Object.compile (\node_modules\ejs\lib\ejs.js:366:16)
at handleCache (\node_modules\ejs\lib\ejs.js:215:18)
at tryHandleCache (\node_modules\ejs\lib\ejs.js:254:16)
at View.exports.renderFile [as engine] (\node_modules\ejs\lib\ejs.js:459:10)
at View.render (\node_modules\express\lib\view.js:135:8)
at tryRender (\node_modules\express\lib\application.js:640:10)
at Function.render (\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (\temp\node_modules\express\lib\response.js:1012:7)

这是我位于根目录的 main.js 代码

express = require("express");
layouts = require("express-ejs-layouts");
app = express();
app.set("port",process.env.PORT || 3000);
app.set("view engine", "ejs");
const homeController = require("./controllers/homeController");
app.use(layouts);
app.get("/users/:userName", homeController.respondWithuName);
app.listen(app.get("port"), () => {
console.log(`Server is running on blah blah port: ${app.get("port")}`);
});

这是我的index.ejs,位于/views

<h1> Hello, <%= uname %></h1>

这是我的layout.ejs,也位于/views

<body>
<%- include partials/header.ejs %>
<%- body %>
<%- include partials/footer.ejs %>
</body>

我的页眉和页脚 ejs 文件位于/views/partials 中,目前基本上只是带有各自 ID 的 div。

这是位于根目录中的 package.json,它显示我安装了所有正确的依赖项

 {
"name": "test",
"version": "1.0.0",
"description": "test",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "test"
},
"author": "test",
"license": "ISC",
"dependencies": {
"ejs": "^3.0.1",
"express": "^4.17.1",
"express-ejs-layouts": "^2.5.0"
}
}

如果我在布局中不使用页眉和页脚的包含,则不会发生错误

所以要么是我做错了什么,要么是 ejs 上的包含有问题。

最佳答案

您必须将部分内容放在括号和引号中。所以你应该写这部分

    <body>
<%- include partials/header.ejs %>
<%- body %>
<%- include partials/footer.ejs %>
</body>

像这样

    <body>
<%- include ("partials/header") %>
<%- body %>
<%- include ("partials/footer") %>
</body>

关于javascript - nodejs/ejs 包含部分语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59237345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com