【问题标题】:sending JSON to address different then res发送 JSON 到不同的地址然后 res
【发布时间】:2019-07-25 00:30:21
【问题描述】:

我创建了一个小型 express 模块。 此 atm 监听 url 以在 localhost 上运行,但计划监听 triggerURL:ListenPort 并从外部服务运行。

clientA:服务器应接收来自网页 (triggerURL) 的调用,并作为响应将 JSON 对象发送到 unity_url

clientB:Unity 应用将打开并监听SendingPort

问题是,虽然我将 JSON 发送到 res 并返回到 clientA 没有问题,但我不确定如何使用 respwritable 创建新的可写流并将 json 发送到 clientB。

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);

【问题讨论】:

    标签: javascript node.js json stream


    【解决方案1】:

    您需要向目标网址发送请求。例如(使用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);
    

    【讨论】:

    • 非常感谢,只是为了确保,我可以将 get 函数中的 '/' 与 'triggerURL' 交换来设置调用此函数所需的 url,对吗?
    • 这取决于您的triggerURL 的外观。例如:google.com/hello/world 不起作用,但 /hello/world 会。
    • 好吧,触发器也来自外部 url/IP(即 clientA)。那么我在哪里插入IP以便我可以从那里收听?再次感谢
    • 服务器只会“从”它所在的 IP 监听。您无法收听与其他 IP 建立的连接。
    • ahhh 所以就像我将 HTTP 调用发送到 unity 一样,触发器应该将调用发送到我正在收听的地方。因此,如果此服务在另一台计算机上(远程服务)并且我需要从我的计算机上调用它,我是否需要在我的本地计算机上使用另一个服务来触发远程服务上的功能?
    猜你喜欢
    • 1970-01-01
    • 2018-11-11
    • 2017-04-14
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 2012-07-26
    相关资源
    最近更新 更多