【问题标题】:How to send data form first server to other and get other server response in first server?如何将数据从第一台服务器发送到其他服务器并在第一台服务器中获得其他服务器响应?
【发布时间】:2015-10-14 19:23:47
【问题描述】:

我有两台服务器。一台服务器拥有我需要从另一台服务器使用的所有 api。

服务器 1 网址

  • http://127.0.0.1:9000/url(获取、发布、修补、删除)
  • http://127.0.0.1:9000/otherurl(获取、发布、修补、删除)
  • http://127.0.0.1:9000/anotherurl(获取、发布、修补、删除)

服务器 2 网址

  • http://127.0.0.1:4000/first(获取、发布、修补、删除)
  • http://127.0.0.1:4000/second(获取、发布、修补、删除)
  • http://127.0.0.1:4000/third(获取、发布、修补、删除)

我想向服务器 2 发出请求,该服务器向 服务器 1 发出请求,服务器 2 向我发送来自 的响应服务器 1。 我想使用request lib 并通过管道传递数据。

示例(伪代码)

'use strict';

var app = request('express'),
    request = require('request');

app.post('/first', function(req, res, next) {
    request({
        baseUrl: 'http://127.0.0.1:9000/',
        url: 'url',
        method: 'post',
        data: {/* something like req.pipe() */}
    }, function(err, otherRes) {
        res.writeHead(otherRes.statusCode, otherRes.headers);
        otherRes.pipe(res);
    });
});

我该怎么做?

【问题讨论】:

  • 这个问题你解决了吗?

标签: node.js express proxy request server


【解决方案1】:

我认为你可以使用这个中间件connect-modrewrite

app.use(modRewrite([
'http://127.0.0.1:4000/first  http://127.0.0.1:9000/url [P]',
'http://127.0.0.1:4000/second http://127.0.0.1:9000/otherurl [P]',
'http://127.0.0.1:4000/third  http://127.0.0.1:9000/anotherurl [P]'
]));

只需在您的第一个服务器中尝试此代码即可。

【讨论】:

  • 不,我需要使用request lib
猜你喜欢
  • 1970-01-01
  • 2021-07-07
  • 1970-01-01
  • 1970-01-01
  • 2013-12-03
  • 2012-03-10
  • 1970-01-01
  • 1970-01-01
  • 2015-06-25
相关资源
最近更新 更多