node.js从客户端执行服务器端功能 [英] node.js executing server side function from client side
问题描述
我在服务器端有server.js文件,在客户端有index.html。我需要从服务器端调用一个函数,可能是index.html上的一个按钮或者某个东西,并在index.html上获得一个函数执行的feedbak。
I have the server.js file on server side and the index.html on client side. I need to call a function from the server side through maybe a button or something on the index.html and get a feedbak on the index.html that the function was executed.
将来我还想从客户端向服务器端传递一个变量。
In the future I would also like to pass over a variable to the server side from client side.
我对node.js比较新。希望有人可以帮助我。
I'm rather new to node.js. Hopefully someone can help me on this.
推荐答案
Node.js与任何其他服务器端技术没什么不同:它不是客户端。
Node.js is no different than any other server side technology: it's not client side.
如果你想在服务器上执行某些操作,你有两种方法:
If you want to execute something on the server, you have two ways:
- 提交表格。这将重新加载页面,您可以选择在同一页面上重定向。
- 从客户端运行ajax请求以执行服务器端的操作。
第三种方式(感谢socket.io)正在使用websockets。不过,如果你不知道它是什么,我不会建议你使用它们。学习HTTP,然后学习websockets。
A third way (thanks to socket.io) is using websockets for your purpose. I wouldn't suggest that you use them if you don't know what it is, though. Learn HTTP, then websockets.
这篇关于node.js从客户端执行服务器端功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!