【问题标题】:Node.js proxy response callbackNode.js 代理响应回调
【发布时间】:2016-12-19 14:06:53
【问题描述】:

我希望能够将某些东西从我的机器卷曲到我的服务器。服务器应该向另一台机器发送另一个请求,并以该响应进行响应。类似代理的东西。所以现在我有(某些部分由于原因被混淆了):

internalHttpServer = _Http.createServer(function (request, response) {
   if(request is valid){
        pass_on_request(request, response); /** Sends the request on another server **/
        respond_with_data(request, response); /** how do I write this function to respond with the other result? **/
      }
  }
  respond_400(response);
});

function pass_on_request(req, res) {
 var id = get_id_to_request_from()
 list_of_other_connections[id].sendUTF(request)
 /** How do I send res with the results of this call? **/


connection.on('message', function(message) {
 /** what to do **/
});

提前致谢

【问题讨论】:

    标签: javascript node.js curl callback


    【解决方案1】:

    您需要使用像 socket.io 这样的库来保持连接打开。当它结束时,它也可能在 HTTP2 中可用。据我所知,使用 express 无法做到这一点,因为它希望从一个请求中得到一个答案。

    【讨论】:

    • 好的,但是other_connection 为我要发送到的设备保持一个永久通道。所以我不能听onClose. .. 另外,res 是第一个回调
    • 是的,我后来看到了那个更新。您应该可以使用 list_of_other_connections[id].on('message'... 而不是 close
    • 等一下,我现在看到您的问题,我想在这种情况下您不能使用 restful API,因为它只发送一个响应。您需要使用诸如 socket.io socket.io 之类的东西保持连接打开
    • 我考虑了这一点,并得出结论,我可能需要保持一条线路畅通,直到完成。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    • 2012-11-27
    • 1970-01-01
    相关资源
    最近更新 更多