【发布时间】:2019-06-19 12:59:07
【问题描述】:
我正在尝试将不使用 node.js 的应用程序转换为节点格式。
最初的应用程序只包含一个code.js 文件和index.html 文件,如下所示:
<html>
<head>
...
<script type="text/javascript" src="code.js"></script>
</head>
<body onload="load()">
(...)
<div style="position:absolute; top:250px; left:20px">
<input type="button" onClick="init()" value="New game">
(...)
</body>
</html>
为了将其转换为节点格式,我将 index.html 重命名为 views/index.ejs,并将 js 函数替换为 ejs 引号:
<html>
...
<body onload="<%= load %>()">
(...)
<div style="position:absolute; top:250px; left:20px">
<input type="button" onClick="<%=init %>()" value="New game">
(...)
</body>
</html>
我还在 code.js 文件的末尾添加了以下内容:
module.exports={
load:load,
init:init
}
最后,在我的 app.js 主节点文件中,我有以下相关的 sn-p :
var exportedValues = require('./code.js');
app.get('/', (req,res) => {
res.render('index.ejs',exportedValues);
});
这几乎可行:当我检查控制台时,ejs 引号确实被 load 和 init 函数的定义替换,但作为这些定义,反过来,使用 code.js 中定义的其他函数,浏览器不知道如何处理它们。
这样做的正确方法是什么?
【问题讨论】:
-
我不确定您将其转换为“节点格式”是什么意思...节点是在服务器上运行的 JavaScript...HTML(其中的 JS)在浏览器中运行。您不能在浏览器中运行 Node 代码。
标签: node.js mouseevent dom-events