简体   繁体   English

在node.js中通过本地主机进行数据交换

[英]data exchange via localhost in node.js

in the following code, app1.js sends information on localhost port 3000 在以下代码中,app1.js在本地主机端口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. 我想制作第二个程序app2.js,该程序将同时运行并读取localhost:3000上app1.js发送的数据。

How can I do that ? 我怎样才能做到这一点 ?

Thank you for your help 谢谢您的帮助

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. 但是,如果您需要发送更多数据,则可能必须设置另一台服务器,或者做一些更复杂的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM