SSE (Server Sent Events) , 在 Node.js 服务器中监听一个事件并 POST 它到其他服务器

SSE (Server Sent Events) , Listen an event in Node.js server and POST it to other server

我有由 SSE(服务器发送事件)触发的事件侦听器,我如何 post 将数据发送到其他服务器。

NODE JS

    //including Event Source
    var EventSource = require('eventsource');       

    var es  = new EventSource('http://api.xyz.com:8100/update/');

    //Listening URL Event Sourse 
    es.addEventListener('message', function (e) { 

        //Extract Json
        var extractData =  JSON.parse(e.data); 

        if(extractData.type == 'CALL' )
        {  
           console.log(extractData);
           //POST DATA
        }
    });

我需要post其他服务器另一端的数据是PHP

你可以试试请求库,相关问题:你可以参考这个

//including Event Source
var EventSource = require('eventsource');

var request     = require('request');  /*****ADDED******/   

var es  = new EventSource('http://api.xyz.com:8100/update/');

//Listening URL Event Sourse 
es.addEventListener('message', function (e) { 

    //Extract Json
    var extractData =  JSON.parse(e.data); 

    if(extractData.type == 'CALL' )
    {  
       console.log(extractData);

        //you can call a function 
        postDataToServer(extractData); 

    }
});

//Post Details to other server
function postDataToServer(SendingDetails)
{ 
    var ServerURL = 'OTHER_SERVAL_URL';

    request({
        url    : ServerURL,
        method : "POST",
        json   : true,
        body   : SendingDetails
    }, function (error, response, body){
        console.log(body);       
    });
}