0

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

droledenom
  • 207
  • 2
  • 6
  • 18
  • Create another server to receive your request and take a look at [How to make an HTTP POST request in node.js?](https://stackoverflow.com/a/12999483/8298495) – Jarek Kulikowski Jul 22 '17 at 19:32
  • It is necessary to make another server ? There is no way to "listen" a port and collect the information ? – droledenom Jul 22 '17 at 19:55
  • Ah, perhaps you could use a child_process and call it directly, I haven't tried that, but it might be an option. – Jarek Kulikowski Jul 22 '17 at 20:01

1 Answers1

0

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.

Jarek Kulikowski
  • 1,399
  • 8
  • 9