user1691445
user1691445

Reputation: 11

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 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.

I'm rather new to node.js. Hopefully someone can help me on this.

Upvotes: 1

Views: 3031

Answers (3)

Florian Margaine
Florian Margaine

Reputation: 60717

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.

Upvotes: 3

DanBaker
DanBaker

Reputation: 582

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.

Upvotes: 1

skovalyov
skovalyov

Reputation: 2099

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.

Upvotes: 0

Related Questions