Seán McGowan
Seán McGowan

Reputation: 89

Using Superagent server-side getting connect ECONNREFUSED Error

I'm building an isomorphic React application. I'm using Webpack to bundle my JS. I'm using react-router for my routing. When I hit the '/search' route, the component to be rendered calls an API (currently just a test JSON file). On the client side, the superagent call works fine and pulls in the data when rendering this route. When I try to render the same route from hitting the server-side, I get this error:

Potentially unhandled rejection [2] Error: connect ECONNREFUSED
    at errnoException (net.js:901:11)
    at Object.afterConnect [as oncomplete] (net.js:892:19)

Any ideas why this is happening?

Here is the code doing the call:

var request = require('superagent');
var Promise = require('Promise');

module.exports = {

  doCall: function(url, params) {

    return new Promise(function(resolve, reject) {

      request
        .get(url)
        .end(function(err, res) {

          if(res) {          
            resolve(res);      
          }
          else {
            reject(err);
          }

      });    

    });

  }

};

Upvotes: 1

Views: 2204

Answers (1)

sqrbrkt
sqrbrkt

Reputation: 46

You might be trying to access the server from itself.

Make sure you're requesting a url on another domain/port.

i.e. doCall('http://example.com/file.json') rather than doCall('/file.json')

Upvotes: 3

Related Questions