I'm sure a 100 people asked this before but I couldn't find the right key words to google it.
Simply put: How do I make a web client send information to the web server the right way?
I have a simple website set up with apache 2 on a raspberry with ubuntu. Something very basic. I'd like to have a button on my website that makes my server run a script (I wrote that script in c but I don't care if I have to translate it). One solution is using JS client-side to send a message to my server on a specific port (say 50000), and having the server listen on that port with a custom listener.
That works fine, but I'm sure there is a right way to do this. How should I do this so that people won't be pissed by my architecture too much? ( using other ports than 80 and 443 on browser may not work if the client blocks other ports)
CodePudding user response:
What you want to do is create a RESTful web API. The server can listen on a specific port and handle different HTTP requests in different ways. You will want to use Controllers, Services, and potentially a Data Access Object layer. You could either have the script you want to run in code, or you could have your code simply make a call to execute a bash or shell script once you get a valid request of the correct type (GET, POST, PATCH, PUT, etc) with the correct parameters.
https://www.tutorialspoint.com/nodejs/nodejs_restful_api.htm
You're correct, it would be a bad decision to try to have a single endpoint on one port do one thing, and another endpoint on another port do something else. First, a single application can only listen on a single port (or at least should only listen on a single port), so you'd need to spin up a new application for everything you want your back-end server to do. Second, you can't be semantic. Your users would have to look at a dictionary of port - action mappings, instead of being able to (for example) send a request to yourService.com/run/script/1234
to run a script with ID of 1234.
Here is a bit of information on HTTP requests to get you started: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods