我正在尝试打包使用 epxress 和 ejs 的 nodejs 应用程序,但在此过程中出现包裹错误
如果我使用标准方式,我的应用程序已经按预期运行:
node app.js
但是,当我尝试使用 parcel 运行它时出现错误。
我的 package.json(只显示相关部分):
"scripts": {
"start": "parcel views/index.ejs",
"build": "parcel build --public-url . views/index.ejs"
},
"dependencies": {
"parcel-plugin-ejs": "^0.2.0",
"parcel-plugin-ejs-template": "^0.1.1",
},
"devDependencies": {
"parcel-bundler": "^1.12.3"
}
这就是我呈现页面的方式:
router.get("/" , function(req, res) {
(async () => {
const client = await pool.connect()
try {
const result = await client.query('select id, name from mytable')
res.render("index", {
items: result.rows
})
} finally {
client.release()
}
})().catch(e => console.error(e.stack))
});
和我的 index.ejs 文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>My website</title>
</head>
<body>
<div class="menu">
<% items.forEach(function(row){ %>
<div class="item" data-value="<%= row.id %>"> <%= row.name %></div>
<% }) %>
</div>
.
.
.
.
</body>
</html>
我希望我的应用程序能够成功打包并能够通过提供的 url 访问它。
但是,我收到了这个错误:
$ npm run start
> my_project@1.0.0 start /home/user/my_project
> parcel views/index.ejs
Server running at http://localhost:1234
🚨 /home/user/my_project/views/index.ejs: items is not defined
at eval (eval at compile (/home/user/my_project/node_modules/ejs/lib/ejs.js:618:12), <anonymous>:5:8)
at returnedFn (/home/user/my_project/node_modules/ejs/lib/ejs.js:653:17)
at EjsAsset.generate (/home/user/my_project/node_modules/parcel-plugin-ejs-template/EjsAsset.js:30:12)
at <anonymous>
其中 items 只是我使用 res.render() 从后端发送以创建下拉列表的产品列表。
如有任何帮助,我们将不胜感激。
最佳答案
如果您更改 start 和 build 脚本的部分,您有 views/index.ejs 用于 index。 ejs
关于node.js - 有没有办法打包使用 express 和 ejs 的 nodejs 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56815093/