简体   繁体   English

如何在单击按钮时运行特定于节点的 function

[英]How do i run a node specific function on button click

I'm trying to change the context of a text file on an HTML button click.我正在尝试更改 HTML 按钮单击上的文本文件的上下文。 I have this so far:到目前为止我有这个:

index.html :索引.html

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Table</title>
    <script src="node-main.js"></script>
</head>
<body>
    <embed src="table.txt">
    <button onclick="Update()">Update</button>
</body>
</html>

node-main.js :节点main.js

const fs = require('fs');

function Update() {
    fs.writeFileSync("table.txt", 'New text');
}

It does not do what I was expecting.它没有达到我的预期。 It gives me the error: require is not defined它给了我错误: require is not defined

Thanks in advance:)提前致谢:)

You should run node-main.js as a server that receives fetch requests from your client.您应该将 node-main.js 作为从客户端接收 fetch 请求的服务器运行。 You're trying to run server-side scripts on the client.您正在尝试在客户端上运行服务器端脚本。

Try using Express.js尝试使用 Express.js

https://expressjs.com/ https://expressjs.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM