我有一个由SSE触发的事件监听器(服务器发送事件),我怎样才能将数据发布到其他服务器。
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
}
});
我需要将数据发布到其他服务器,另一端是PHP
答案 0 :(得分:2)
您可以尝试使用请求库,相关问题:how to make an http post request in node js您可以参考此内容。
//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);
});
}