简体   繁体   中英

Use button click to send message to a node server

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

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. Ie creating a route and handler about it. The other method is via web sockets. Ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM