Javascript 使用 Express、Node.JS 和 Require 模块调用外部 API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39301227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:26:13  来源:igfitidea点击:

External API Calls With Express, Node.JS and Require Module

javascriptnode.js

提问by Dave Melia

I have a route as followed:

我有一条路线如下:

var express = require('express');
var router = express.Router();
var request = require('request');

router.get('/', function(req, res, next) {
  request({
    uri: 'http://www.giantbomb.com/api/search',
    qs: {
      api_key: '123456',
      query: 'World of Warcraft: Legion'
    },
    function(error, response, body) {
      if (!error && response.statusCode === 200) {
        console.log(body)
      }
    }
  });
});

module.exports = router;

I'm trying to make an API call to the Giant Bomb API to bring back whatever data it has about World of Warcraft.

我正在尝试对 Giant Bomb API 进行 API 调用,以带回有关魔兽世界的任何数据。

The problem is, the route just loads; it doesn't do anything or it doesn't time out, it's just continuous loading.

问题是,路由只是加载;它什么也不做,也没有超时,它只是持续加载。

I don't know what I'm doing wrong, but that being said... I don't know what's right either. I'm trying to learn as I go along.

我不知道我做错了什么,但话说回来……我也不知道什么是对的。我正在努力学习。

Any help would be great.

任何帮助都会很棒。

Thanks

谢谢

回答by jfriend00

You need to take the data you get from request()and send it back as the response to the original web server request. It was just continuously loading because you never sent any sort of response to the original request, thus the browser was just sitting there waiting for a response to come back and eventually, it will time out.

您需要获取从中获取的数据request()并将其作为对原始 Web 服务器请求的响应发送回去。它只是不断加载,因为您从未对原始请求发送任何类型的响应,因此浏览器只是坐在那里等待响应回来,最终它会超时。

Since request()supports streams, you can send back the data as the response very simply using .pipe()like this:

由于request()支持流,您可以非常简单地使用.pipe()如下方式将数据作为响应发回:

var express = require('express');
var router = express.Router();
var request = require('request');

router.get('/', function(req, res, next) {
  request({
    uri: 'http://www.giantbomb.com/api/search',
    qs: {
      api_key: '123456',
      query: 'World of Warcraft: Legion'
    }
  }).pipe(res);
});

module.exports = router;

This will .pipe()the request()result into the resobject and it will become the response to the original http request.

这将.pipe()request()结果到res对象,它会成为原来的HTTP请求的响应。

Related answer here: How to proxy request back as response

相关答案在这里:如何将请求代理回作为响应

回答by rishat

Per every route in Express, it is necessary to send a response (partial or complete) or call next, or do both. Your route handler does neither. Try

对于 Express 中的每条路由,都需要发送响应(部分或完整)或调用next,或两者都执行。您的路由处理程序两者都不做。尝试

var express = require('express');
var router = express.Router();
var request = require('request');

router.get('/', function(req, res, next) {
  request({
    uri: 'http://www.giantbomb.com/api/search',
    qs: {
      api_key: '123456',
      query: 'World of Warcraft: Legion'
    },
    function(error, response, body) {
      if (!error && response.statusCode === 200) {
        console.log(body);
        res.json(body);
      } else {
        res.json(error);
      }
    }
  });
});

module.exports = router;

and see what data this route handler responds with.

并查看此路由处理程序响应的数据。