【发布时间】:2019-12-24 10:15:35
【问题描述】:
我创建了一个带有 2 个按钮的 html 页面。当我单击这些按钮时,我想调用一个 node.js 文件。当前,当我单击此按钮时,什么都没有发生。当我检查 chrome 中的页面时,它显示了
加载资源失败:服务器响应状态为 404(未找到)
我已经使用命令npm install -g express 安装了 express。我在 ubuntu 上的 apache2 服务器上运行它。我已将 html 和 nodejs 文件复制到 var/www/html 目录。
我使用了 express 框架,并且正在使用 ajax 请求与 nodejs 文件进行通信。
web.html
<!DOCTYPE html>
<html>
<h3>Example of a Deploy And Decommission Button</h3>
<body>
<button onclick="myFunction()" >Deploy</button>
<button onclick="myFunction()">Decommission</button>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script >
function myFunction() {
$.post("/createContainer").done(() => {
console.log("container created")
});
}
</script>
</body>
</html>
node.js 文件
const app = require("express")();
const process = require('child_process');
app.post("/createContainer", (req, res) => {
process.exec('kubectl apply -f tomcat.yaml',function (err,stdout,stderr) {
if (err) {
console.log("\n"+stderr);
} else {
console.log(stdout);
}
});
});
app.list(80);
如果有的话,我必须对我的代码进行哪些更改。我听说 node.js 将在它自己的节点服务器上运行,这让我想到了如何在同一台服务器上同时运行 html 页面和 nodejs 的下一个问题.
我的意思是我真的很困惑我将如何运行这个应用程序。请帮忙。
【问题讨论】: