在html文件中调用node.js函数 [英] Call a node.js function inside an html file

查看:313
本文介绍了在html文件中调用node.js函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很可能是重复的问题.

This is most likely a repeated question.

我最接近答案的地方是:执行Nodejshtml页面中的脚本?

The closest I got to an answer was here: execute a Nodejs script from an html page?

但是,我还是听不懂.

这就是这种情况:

我有一个包含以下文件的快速服务器设置:

I have an express server setup with the following files:

  • 快递应用

server.jsindex.html

server.js index.html

现在,我希望html文件夹中有一个按钮,该按钮调用在node.js文件中设置的功能.

and right now I want the html folder to have a button, that calls a function set in the node.js file.

如果需要更多信息,请告诉我,谢谢!

Tell me if more information is needed, thanks!

我正在重新提出问题,以使其更加清晰.

Im remaking the question to be more clear.

我正在使用快递服务器来展示一个网站,该网站上显示一个表示关闭电源"的按钮,我希望该按钮能够在我的服务器计算机上执行一项操作,该操作是将其关闭电源的终端命令

I am using an express server to present a website, that present a button saying " Power Off", I want this button to be able to execute an action on my server computer, that action being a terminal command to power it off.

我想知道如何使用HTML编写的,托管在服务器上但呈现给客户端的按钮与托管在服务器上的server.js文件进行交互,该文件应具有执行设置的功能.命令.

I wanted to know how could I make said button, written in HTML, hosted on the server but presented to the client, to interact with the server.js file hosted on the server, which would have a function set to execute said command.

谢谢!

推荐答案

您需要更好地了解网页的客户端/服务器体系结构如何工作以及代码实际在何处运行以及客户端和服务器之间如何通信

You need to understand a little better how the client/server architecture of a web page works and where code actually runs and how the client and server communicate with one another.

您不能直接从HTML文件在node.js服务器上调用函数.HTML文件位于客户端的浏览器中.节点服务器是您的Web服务器,与其他计算机上客户端浏览器的距离很远.尽管HTML似乎位于您的node.js服务器上,因为它位于该服务器上的目录中,但这仅是它的存储位置.当浏览器请求该页面时,您的node.js服务器将HTML发送到客户端的浏览器,并在客户端浏览器中将其呈现回去,这就是该页面中Javascript的运行位置(在客户端的浏览器中,远离您的node.js.服务器).这是一个客户端-服务器体系结构.HTML页面正在客户端上运行.node.js服务器在您的服务器上-不同的计算机上.

You can't call a function directly on your node.js server from an HTML file. The HTML file is in the client's browser. The node server is your web server, far away from the client's browser on different computers. Though it may seem like your HTML is on your node.js server because it's in a directory on that server, that's only where it is stored. When the browser requests that page, your node.js server sends the HTML to the client's browser and it is rendered back in the client browser and that's where the Javascript in that page runs (in the client's browser, far away from your node.js server). This is a client-server architecture. The HTML page is running on the client. The node.js server is on your server - different computers.

如果要与Web页面上的node.js服务器通信,则可以使用HTML页面中的Javascript从Web页面中的Javascript到node.js服务器进行Ajax调用(Ajax调用是http请求).然后,您可以在node.js服务器中为该特定的Ajax调用配置路由,然后可以在node.js中编写代码以执行收到该Ajax调用时想要执行的任何操作.它可以在服务器上执行某些操作,可以检索数据并将其返回给客户端,等等...您可以选择使用Ajax调用发送数据(作为GET请求的查询参数或POST的正文数据)请求),然后服务器可以选择将数据返回给您(通常为JSON,但可以是您喜欢的任何格式).

If you want to communicate with the node.js server from the web page, then you use Javascript in the HTML page to make an Ajax call from the Javascript in the web page to the node.js server (An Ajax call is an http request). You then configure a route in the node.js server for that specific Ajax call and you can then write code in node.js to do whatever you want to happen when that Ajax call is received. It can carry out some operation on the server, it can retrieve data and return it to the client, etc... You can optionally send data with the Ajax call (either as query parameters for a GET request or as body data for a POST request) and then the server can optionally return data back to you (often as JSON, but it can be any format you like).

这篇关于在html文件中调用node.js函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆