Skip to content Skip to sidebar Skip to footer

Node.js Executing Server Side Function From Client Side

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

Solution 1:

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:

  • Submit a form. This will reload the page and you can choose to redirect on the same page it was.
  • Run an ajax request from the client side to do something server side.

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.

Solution 2:

This is a great question. I have done this using Socket.io (http://socket.io/)-- which is a nice library that allows sending data from the client (index.html) to the server, and allowing the server to send data to the client. Look at the examples on socket.io site.

Solution 3:

Well, to make it simple, your function should be "exposed" through some URL. You can use http://expressjs.com/ framework to speed up server side application creation. Once you have that and your function is available at let's say http://localhost/myFunction?myParameter=myValue, you can use jQuery call $.json() to invoke that function from your client side application, i.e. your index.html.

Post a Comment for "Node.js Executing Server Side Function From Client Side"