0

I'm using Node.JS with Express (and ejs for the views) and I want to call two API endpoints to render them in the same page and use the data. I've tried doing it with async but I get

ECONNRESET: Request could not be proxied!

This is my code

 app.get('/profile', function(req, res) {
    async.parallel([
    function(next) {
       var query = req.query.search;
       var url = '1st url' + query;
       var request = require('request');
        request(url, function(error, body) {
           var data = JSON.parse(body);
            next(error, data);
        });
    },
    function(next) {
        request('2nd url', function(error, tlist) {
            var list  = JSON.parse(tlist);
            next(error, list);
        });
    }], function(err, results) {
      if (!err && results.statusCode == 200)

           var data = results[0];
           var list = results[1];


        res.render('profile', {data: data, list: list});
    });
});
4
  • Are you sitting behind a proxy? ECONNRESET means the connection has been closed from the other side. You should check your HTTP_PROXY/HTTPS_PROXY environment variables. Commented Oct 27, 2016 at 21:34
  • Hi, nope. In fact I am using Cloud9's online IDE. The issue arises when I parse the data and then when I try and break the callback result array and assign it to my variables. I want to use the two JSON files independently from each other in the web page. Commented Oct 27, 2016 at 21:37
  • First figure out which of the two requests causes the error (my money is on the first). Try to run it from your local machine too. Otherwise post the URL, so we can check. Commented Oct 27, 2016 at 21:53
  • There's another problem with how you include the request module. Move the require('request') line to the top of the file. Commented Oct 27, 2016 at 21:54

1 Answer 1

1

Unsure about Cloud9, but if the issue is around parsing data, there's a couple of things here.

You should handle the error on each request before you attempt to parse; if parse throws an exception, your callback won't be executed:

    request(url, function(error, body) {
      if (error) return next(error);

      var data = JSON.parse(body);

      next(null, data);
    });

You should probably also have a try/catch around the parse, and execute your callback with an error if there's an exception:

    request(url, function(error, body) {
      if (error) return next(error);

      var data;

      try {
        data = JSON.parse(body);
      } catch (e) {
        return next(new Error('Unable to parse body for ' + url));
      }

      next(null, data);
    });

Finally, your current check for results.statusCode will always return false, since it's an array, so you won't reach the end of the request. I'm guessing this is probably where the problem lies. I would also recommend passing any errors from async on to the Express error handler:

function(err, results) {
  if (err) {
    // pass to Express error handler...
  }

  var data = results[0];
  var list = results[1];

  res.render('profile', {data: data, list: list});
});
Sign up to request clarification or add additional context in comments.

3 Comments

Also as svens pointed out, you'll want the require('request') up at the top
Hey, thanks for the pointers. I did all of the above and tried running the server again, I got the following error Error: Unable to parse body for <url> at Request._callback The API endpoints work fine on their own, it's just when I try to call them both at the same time that this happens. I need to use both JSON files in my web app, but independently, so having both of them in an object would be counterproductive I think.
That means the body is not parsable. If I remember correctly I think request passes its body as the third parameter, so your callback should probably be something like request(url, (err, httpResponse, body) => {}). May be worth trying to log out the body before you parse it to get a peek at what it's like.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.