Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

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

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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 res object and it will become the response to the original http request.

Related answer here: How to proxy request back as response


Edit in 2021. The request() library has now been deprecated and is no longer recommended for new code. There are many alternatives to choose from. My favorite is the got() library. The above could be accomplished using it like this. This also upgrades to use the pipeline() function which is a better version of .pipe() with more complete error handling.

const router = require('express').Router();
const got = require('got');
const { pipeline } = require('stream');

router.get('/', function(req, res) {
  const dataStream = got.stream({
      uri: 'http://www.giantbomb.com/api/search',
      qs: {
        api_key: '123456',
        query: 'World of Warcraft: Legion'
      }
  });
  pipeline(dataStream, res, (err) => {
      if (err) {
          console.log(err);
          res.sendStatus(500);
      }
  });
});

module.exports = router;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...