【问题标题】:ExpressJS - contact external APIExpressJS - 联系外部 API
【发布时间】:2012-04-20 03:29:36
【问题描述】:

事情是这样的: 我有一个将数据发送到服务器的客户端。此服务器必须联系外部 A.P.I.并将其响应发送回客户端。一旦服务器获得客户端数据,我就是不知道如何以及在何处联系外部 A.P.I。

我这样路由客户端数据:

app.post('/getAutoComplete', routes.read);

routes.read 检索 req.body 中的数据。使用我的 nodejs 版本(没有 express 框架),然后我以这种方式请求 api:

var http = require('http'), options = {
        host : "192.168.1.38",
        port : 8080,
        path : "/myURL",
        method : 'POST'
};

var webservice_data = "";

var webservice_request = http.request(options, function(webservice_response)
{
    webservice_response.on('error', function(e){ console.log(e.message); });
    webservice_response.on('data', function(chunk){ webservice_data += chunk;});
    webservice_response.on('end', function(){res.send(webservice_data);});
});

webservice_request.write(req.body);
webservice_request.end();

问题是我想使用原生 expressJS 方法,比如 app.post,但我不知道怎么做,因为:

  1. Express (app) 对象在此处不​​可用(在 app.js 中声明但不在路由文件中)
  2. 我不知道如何使用 app.post 发送 POST 数据

有什么建议吗?

【问题讨论】:

  • 这不是 Express 的工作方式。 “像 app.post 这样的原生 expressJS 方法”用于接收 HTTP 请求,而不是发送它们。
  • 那么我应该如何联系外部 A.P.I.使用 expressJS 吗?和我现在做的一样吗?

标签: node.js express


【解决方案1】:
app.post('/getAutoComplete', routes.read);
// assuming routes.read lookes something like this
routes.read = function read(req, res) {
  var http = require('http'), options = {
          host : "192.168.1.38",
          port : 8080,
          path : "/myURL",
          method : 'POST'
  };

  var webservice_data = "";

  var webservice_request = http.request(options, function(webservice_response)
  {
      webservice_response.on('error', function(e){ console.log(e.message); });
      webservice_response.on('data', function(chunk){ webservice_data += chunk;});
      webservice_response.on('end', function(){res.send(webservice_data);});
  });

  webservice_request.write(req.body);
  webservice_request.end();
};

还可以查看 https://github.com/mikeal/request 它是在节点中执行 Web 请求的事实上的模块。

【讨论】:

    【解决方案2】:

    routes.read 是一个函数。您可以使用额外的参数调用它,例如

    app.post('/getAutoComplete', function(req,res) {
        var q = req.query.q;  // or whatever data you need
        routes.read(q, function(err, response) {
            if (err) throw err;
            return res.json(response);
        });
    });
    

    现在让routes.read 函数使用第一个参数作为查询,当它从远程 API 收集响应时,调用带有任何错误的第二个参数作为第一个参数,并将响应作为第二个参数。

    更新这个答案已经被选为答案,但如果我也展示一个 routes.read 的例子会更有帮助:

    routes.read = function(q, cb) {
        // pretend we calculate the result
        var result = q * 10;
        if (result > 100) {
            // call the callback with error set
            return cb("q value too high");
        }
        // all is well, use setTimeout to demonstrate
        // an asynchronous return
        setTimeout(function() { cb(null, result) }, 2000);
    };
    

    【讨论】:

      猜你喜欢
      • 2021-01-19
      • 2015-10-16
      • 1970-01-01
      • 2018-02-22
      • 1970-01-01
      • 2021-12-08
      • 1970-01-01
      • 2018-04-12
      • 2010-10-24
      相关资源
      最近更新 更多