This is a broader question than can be given a detailed answer here. Your first step is to work out what you want your server side API to to look like - normally I'd suggest a REST architecture, but it only semi-applies here. In any case you need to define the URI scheme, that the client will contact. I'd suggest something like:
http://hostname/calculator/add?param1=10¶m2=5
On an HTTP GET - as you are getting the result as opposed. I'd strongly recommend the node package express for this work:
Node.js Express Framework[
^]. To set up the endpoint you'd do something like:
app.get('/calculator/add', function (req, res) {
response = {
result: req.query.param1 + req.query.param2,
};
res.end(JSON.stringify(response));
})
The above will probably need some fettling - not sure whether param1 & param2 will result in a string concat for example or not.
You can test this by calling the URL via the chrome extension "Postman" (
Postman - Chrome Web Store[
^]). Even this scheme has deficiencies - you're tying the client in to providing parameters - you might want to be also able to support the schema
http://hostname/calculator/add/10/5
Also you should think about HTTP status codes and how to handler errors etc.
The other thing "missing" with this simple code is the we're assuming the client wants JSON data - reading the request "Accept" header is more accommodating, also we should set the response header "Content-Type" to reflect the fact we're serving JSON (or whatever, if we're honouring the request Accept).
To call from the client you can just use a form (you might need to change the scheme - forms tend to work with POST), or use
XMLHttpRequest object[
^] to form the call call to the server and use it's onstatechange event to get the response. Despite it's name, this object is used to send/get JSON responses. Again, the detail of this depends on your schema.
The final thing is watch out for CORS problems - you probably won't get these with postman, then suddenly your server stops responding when you get the client going. The client will probably send an OPTIONS request first, which will fail as your server prevents CORS requests - you need to code your server to allow requests from any origin.
Lots to think about! - Step one is getting a server responding to requests from postman, I'd just start with one that answers a plain-text "hello world".