如何向 3rd 方 API 编写 Node.js 请求?

发布于 2024-10-26 17:52:58 字数 155 浏览 2 评论 0原文

有没有人有一个 API 响应从第 3 方发出的 http.request() 传回我的 clientSever 并写出到客户端浏览器的示例?

我一直陷入我确信简单的逻辑之中。我通过阅读文档使用express,它似乎没有为此提供抽象。

谢谢

Does anyone have an example of an API response being passed back from a http.request() made to a 3rd party back to my clientSever and written out to a clients browser?

I keep getting stuck in what I'm sure is simple logic. I'm using express from reading the docs it doesn't seem to supply an abstraction for this.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最美的太阳 2024-11-02 17:52:58

请注意,这里的答案有点过时了——您会收到一条已弃用的警告。 2013 年的等效内容可能是:

app.get('/log/goal', function(req, res){
  var options = {
    host : 'www.example.com',
    path : '/api/action/param1/value1/param2/value2',
    port : 80,
    method : 'GET'
  }

  var request = http.request(options, function(response){
    var body = ""
    response.on('data', function(data) {
      body += data;
    });
    response.on('end', function() {
      res.send(JSON.parse(body));
    });
  });
  request.on('error', function(e) {
    console.log('Problem with request: ' + e.message);
  });
  request.end();
});

如果您要编写大量此类内容,我还会推荐 request 模块。从长远来看,它会为您节省大量击键次数!

Note that the answer here is a little out of date-- You'll get a deprecated warning. The 2013 equivalent might be:

app.get('/log/goal', function(req, res){
  var options = {
    host : 'www.example.com',
    path : '/api/action/param1/value1/param2/value2',
    port : 80,
    method : 'GET'
  }

  var request = http.request(options, function(response){
    var body = ""
    response.on('data', function(data) {
      body += data;
    });
    response.on('end', function() {
      res.send(JSON.parse(body));
    });
  });
  request.on('error', function(e) {
    console.log('Problem with request: ' + e.message);
  });
  request.end();
});

I would also recommend the request module if you're going to be writing a lot of these. It'll save you a lot of keystrokes in the long run!

机场等船 2024-11-02 17:52:58

以下是在快速获取函数中访问外部 API 的快速示例:

app.get('/log/goal', function(req, res){
    //Setup your client
    var client = http.createClient(80, 'http://[put the base url to the api here]');
    //Setup the request by passing the parameters in the URL (REST API)
    var request = client.request('GET', '/api/action/param1/value1/param2/value2', {"host":"[put base url here again]"});


    request.addListener("response", function(response) { //Add listener to watch for the response
        var body = "";
        response.addListener("data", function(data) { //Add listener for the actual data
            body += data; //Append all data coming from api to the body variable
        });

        response.addListener("end", function() { //When the response ends, do what you will with the data
            var response = JSON.parse(body); //In this example, I am parsing a JSON response
        });
    });
    request.end();
    res.send(response); //Print the response to the screen
});

希望有帮助!

Here is a quick example of accessing an external API in an express get function:

app.get('/log/goal', function(req, res){
    //Setup your client
    var client = http.createClient(80, 'http://[put the base url to the api here]');
    //Setup the request by passing the parameters in the URL (REST API)
    var request = client.request('GET', '/api/action/param1/value1/param2/value2', {"host":"[put base url here again]"});


    request.addListener("response", function(response) { //Add listener to watch for the response
        var body = "";
        response.addListener("data", function(data) { //Add listener for the actual data
            body += data; //Append all data coming from api to the body variable
        });

        response.addListener("end", function() { //When the response ends, do what you will with the data
            var response = JSON.parse(body); //In this example, I am parsing a JSON response
        });
    });
    request.end();
    res.send(response); //Print the response to the screen
});

Hope that helps!

玩世 2024-11-02 17:52:58

此示例看起来与您想要实现的目标非常相似(纯 Node.js,无 Express):

http://blog.tredix.com/2011/03/partly-cloudy-nodejs-and-ifs.html

HTH

This example looks pretty similar to what you are trying to achieve (pure Node.js, no express):

http://blog.tredix.com/2011/03/partly-cloudy-nodejs-and-ifs.html

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文