简体   繁体   English

告诉 Node.js 服务器运行 function 在 ZFC35FDC70D5FC69D269D3883A822C7A 中使用 javascript

[英]Tell Node.js server to run a function by using javascript in html

I'm trying to send the command to execute a function in Node.js from an html Button and don't really know where to start.我正在尝试从 html 按钮发送命令以在 Node.js 中执行 function 按钮,但我真的不知道从哪里开始。

So I'm currently working on a website that should be able to control my raspberry pi's GPIO pins and send commands to another Linux-Based Computer in the raspberry's network.所以我目前正在开发一个网站,该网站应该能够控制我的树莓派的 GPIO 引脚并将命令发送到树莓网络中的另一台基于 Linux 的计算机。

I already have the functions to do the above completed in javascript and it works perfectly fine when executed in Node.js.我已经在 javascript 中完成了上述功能,并且在 Node.js 中执行时效果很好。 The only Problem is, that when directly implemented into an html, the code is not executed on the raspberry but in the browswer.唯一的问题是,当直接实现到 html 时,代码不是在树莓派上执行,而是在浏览器中执行。 I now know I'll have to host a Node.js server on the Raspberry and send commands to that.我现在知道我必须在 Raspberry 上托管 Node.js 服务器并向其发送命令。 But that exactly is my problem.但这正是我的问题。

server.js: server.js:

function serverShutdown() {

    { some working code }

}

index.html:索引.html:

<html>
    <head>
        <script src="../scripts/client.js"></script>
    </head>
    <body>

        <div class="ButtonClass">
            <button onclick="server_shutdown()">Herunterfahren</button>
        </div>
    </body>
</html>

client.js:客户端.js:

function server_shutdown() {

    ? ? ?

}

I think you need the http module in your server side app to handle an http request from a client to your server.我认为您需要服务器端应用程序中的 http 模块来处理从客户端到服务器的 http 请求。

You could look at this link: https://nodejs.dev/the-nodejs-http-module你可以看看这个链接: https://nodejs.dev/the-nodejs-http-module

From the client side you should call an url that the server could handle.从客户端您应该调用服务器可以处理的 url。 Or you need to create an event handler on the server side.或者您需要在服务器端创建一个事件处理程序。 More informations at: https://www.npmjs.com/package/events更多信息请访问: https://www.npmjs.com/package/events

Moreover your server should have module that exports the methods you need.此外,您的服务器应该具有导出您需要的方法的模块。

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

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