отправка JSON по другому адресу - PullRequest
0 голосов
/ 03 марта 2019

Я создал крошечный экспресс-модуль.этот банкомат прослушивает url для запуска на локальном хосте, но планируется прослушивать triggerURL:ListenPort и запускать из внешней службы.

clientA : сервер должен получить вызов отвеб-страницу (triggerURL) и в ответ отправьте объект JSON на unity_url.

clientB : откроется приложение для единства, которое будет прослушивать SendingPort.

Дело в том, что, хотя у меня нет проблем с отправкой JSON в res и обратно в clientA, я не уверен, как создать новый доступный для записи поток и отправить json в clientB, используя resp и writable.

var express = require('express');
var fs = require('fs');
var app = express();

var triggerURL = ''; //i'll send an http request to this adress to trigger the action from server


var JSON = {
    item: "seeds",
    Answers: "5",
    richText: "<b>How can you reduce crop toxicity by turning plants upside down?</b><br/>Idea:<br/> Upside-down gardening is a hanging vegetable garden being the suspension of soil and seedlings of a kitchen garden to stop <b>pests</b> and blight,and eliminate the typical gardening tasks of tilling, weeding, and staking plants."
}
var port = process.env.PORT || 3000;
var ListenPort = '8086'; // my port to recieve triggers
var SendingPort = '4046'; // which unity will listen to
var unity_url ='185.158.123.54:'+SendingPort; //fake IP, just for the example

//triggerURL
app.get('/', function(req,res){
    var resp = JSON.stringify(JSON);
    var writable = fs.createWriteStream();

    //res.json(JSON); //instead i wanna send it to unity_url;
});


//app.listen(ListenPort);
app.listen(port);

1 Ответ

0 голосов
/ 03 марта 2019

Вам нужно будет отправить запрос на целевой URL.Например (используя node-fetch).

const fetch = require('node-fetch');
const express = require('express');
const app = express();

var JSON = {
    item: "seeds",
    Answers: "5",
    richText: "<b>How can you reduce crop toxicity by turning plants upside down?</b><br/>Idea:<br/> Upside-down gardening is a hanging vegetable garden being the suspension of soil and seedlings of a kitchen garden to stop <b>pests</b> and blight,and eliminate the typical gardening tasks of tilling, weeding, and staking plants."
}

const port = process.env.PORT || 3000;
const unity_url ='185.158.123.54:4046'; //fake IP, just for the example

//triggerURL
app.get('/', function(req,res){
    var resp = JSON.stringify(JSON);

    fetch(unity_url, {
        method: 'post',
        body:    resp,
        headers: { 'Content-Type': 'application/json' },
    })
    .then(res => res.json())
    .then(data => console.log(data));

    res.status(200).send('OK');
});

app.listen(port);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...