0

Probably a very naive question but essentially I am interested in what the title says. There is a node server that was created through http.createServer (serverHandler). The server is presenting a webpage that also has a button. So, the question boils down to: how to send a simple json message to the server when the button is clicked. I mean what sort of javascript code do I need on the webpage in order to send a message / http request to the server?

Assume the message is sent to the address "button.json" and hence it will be received by the node server and handled appropriately (say it prints a message through console.log).

4
  • 1
    possible dublicate? stackoverflow.com/questions/7318962/… Commented Sep 24, 2013 at 12:06
  • Two mini observations: 1. I want to use 'http', not 'net'. 2. How does the client know where to find all these packages mentioned in the commands that are 'require'? Commented Sep 24, 2013 at 12:23
  • Are you OK with using 3rd party client-side libraries like jQuery or AngularJS? Commented Sep 24, 2013 at 12:57
  • @JPRichardson: I guess I can be. I do not know jQuery, so, any pointers in this direction will be appreciated. Thanks for your time. Commented Sep 24, 2013 at 14:03

1 Answer 1

1

The nodejs server is the same as apache for example. It accepts normal http requests. So, the first thing which you may try is to send ajax requests. Of course you should handle the requests. I.e. creating a route and handler about it. The other method is via web sockets. I.e. establish a socket communication between the front-end code and the nodejs server. There are some libraries which could do that. Like http://socket.io/ for example.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your time. If you could point me to a simple example I would appreciate it. Regarding your link, how would load the client script (/socket.io/socket.io.js) to an arbitrary client who is visiting the server? There's something missing from the picture.
/socket.io/socket.io.js is served by the server side of the socket.io library. So, you don't have to care about that. Here are an example krasimirtsonev.com/blog/article/…
Thank you very much for the pointer and the help. I am voting up right now. In a couple of hours if there is no other response I will accept the answer. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.