1

I am trying to get superagent to work on the server side with relative path but it's not playing nicely.

The thought is, I need to proxy from the frontend to the backend with routes /api/* being the proxy route. When doing a superagent request such as:

request.get('/api/surahs')
  .end(function(err, res) {
    debug('SURAHS RECEIVED....');
    console.log(err);
    actionContext.dispatch('surahsReceived', {surahs: res.body, surah: payload});
  });

I always get error

[1] { [Error: connect ECONNREFUSED]
[1]   code: 'ECONNREFUSED',
[1]   errno: 'ECONNREFUSED',
[1]   syscall: 'connect',
[1]   response: undefined }

Any ideas?

Mohamed El Mahallawy
  • 13,024
  • 13
  • 52
  • 84
  • Your API server isn't running? – Adrian Lynch Sep 20 '15 at 08:07
  • It is - in fact it I am logging in the /api/* route and it never reaches there before it calls the API server itself.. – Mohamed El Mahallawy Sep 20 '15 at 08:07
  • Can you hardcode the full URL? So `http://domain.com/api/whatever` rather than `/api/whatever`. If your API isn't logging the request, it unlikely SA is sending it to the right place. – Adrian Lynch Sep 20 '15 at 08:11
  • I did, and it works (as in the apis route hits, makes a call to the api server, returns the call to the api route and then to the initial SA call). But I don't have a hardcoded route cause itll differ from dev, stage, and we have multiple prod – Mohamed El Mahallawy Sep 20 '15 at 08:13
  • I can't see in the docs a way to set the base URL. You may have to resort to doing it manually: `var BASE_URL = /* Workout the base url here */; request.get(BASE_URL + '/api/surahs');` - Which is sucky! – Adrian Lynch Sep 20 '15 at 08:22
  • How do I work out the base url? That's the challenge given you'd normally define your urls in a `constants/Settings.js` – Mohamed El Mahallawy Sep 20 '15 at 08:23
  • Then the question is one of setting environment variables: http://stackoverflow.com/questions/22312671/node-js-setting-environment-variables – Adrian Lynch Sep 20 '15 at 08:25

1 Answers1

0

i ran into same issue. You can get a host from express req like req.get('host').

Than you can create SSR request like: request.get(req.get('host') + '/api/surahs').....

Ivan Kopčík
  • 121
  • 1
  • 1
  • 4