Calling Node.js function from other application

Just out of curiosity I started to play with Node.js, I create a simple server with this code:

var http = require("http");

http.createServer(function(request, response) {
......
}).listen(8888);

Inside the createServer function I have a simple string that I want to return to another app.

The workflow is:

  • Other app send name and lastName
  • Node.js app recieve this data
  • Build the a JSON string with that data
  • Retrieve the JSON string to the other app

Currently I'm hardcoding the name and lastName and building the JSON string with them, but I have all that code on the createServer function, but the problem is that the createServer function is called only on the server startup.

How can I receive and send data from a Node.js app to another app?

When you call http.createServer(), the parameter you pass is actually the function to handle new requests. This function will be called for each and every request. It doesn't run immediately.

Just make the HTTP request from your other application and you will see your function being called.