node.jsdata-exchange

data exchange via localhost in node.js


in the following code, app1.js sends information on localhost port 3000

    //app1.js 
    var http = require('http');
    const valueToTransfert = 'test';
    var server = http.createServer(function(req, res) {
        res.end('valueToTransfert');
    });
    server.listen(3000);

I want to make a second program app2.js that will run simultaneously and read data sent by app1.js on localhost:3000.

How can I do that ?

Thank you for your help


Solution

  • This is a bit of a hack but it might work your your immediate purposes

    require('child_process').exec('node app2.js test', function(err, stdout, stderr) {
        // you get your results in stdout
        // app2.js would have to output its result with console.log(...);
    });
    

    But if you need to send more data you will probably have to setup another server, or do something a bit more complex.