ECONNREFUSED when making GET request in app, but API returns JSON successfully
Asked Answered
W

2

13

I'm writing a node app with React, using node-postgres and superagent for backend calls. Let's say I'm making a GET request and using the JSON it returns to fill a table of students. My API looks like this:

import pg from 'pg';
import Router from 'express';
let router = new Router();
let conString = "postgres://user:pass@localhost/db_name";

router.get('/getStudents', function(req, res) {
  var results = [];

    pg.connect(conString, function(err, client, done) {
      if (err) {
       done();
       console.log(err);
       return res.status(500).json({success: false, data: err});
    }

    var query = client.query('SELECT first_name, last_name, email FROM students');

    query.on('row', function(row) {
      results.push(row);
    });

    query.on('end', function() {
      done();
      return res.json(results);
    });
  });
});

On page load, this is called from the store to set a students array. It seems like something is going wrong here:

var request = require('super agent');

function getStudents() {
   request
     .get('/api/getStudents')
     .set('Accept', 'application/json')
     .end(function(err, res) {
       if (err) {
         console.log("There's been an error: getting students.");
         console.log(err);
       } else {
         return res;
       }
     });
 }

If I curl localhost:3000/api/getStudents, I get the JSON response I expect.

However, when I call this on page load, I get an ECONNREFUSED error:

Error: connect ECONNREFUSED 127.0.0.1:80]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 80,
response: undefined

Not sure why I'm getting an error on the HTTP port. This is my first time using node-postgres, superagent, and React so any help is appreciated.

Edit: Forgot to mention that I'm able to make POST requests and insert items into the database without any errors. This error only occurs when I'm attempting a GET request.

Washrag answered 8/12, 2015 at 2:54 Comment(3)
Did you open up port 80? Seems like super-agent defaults to localhost:80 unless you specify a host and port and seems like you want the port to be 3000.Carden
Do you know how to accomplish that, @Quy? I can't find any documentation or examples of setting the port with superagent. I tried .set('port', 5432) and .set('port', 3000), but no luck so far. Thanks for the suggestion.Washrag
try request.get('http://localhost:3000/api/getStudents')Carden
C
7

Try this (inserting the full path url) in the get method:

request
  .get('http://localhost:3000/api/getStudents')
  .set('Accept', 'application/json')
  .end(function(err, res) {
    if (err) {
      console.log("There's been an error: getting students.");
      console.log(err);
    } else {
      return res;
    }
  });

Check out the documentation for CORS for an example of using absolute urls:

https://visionmedia.github.io/superagent/#cors

Carden answered 11/12, 2015 at 6:51 Comment(1)
Thanks, this worked with how I was implementing it. Unsurprisingly, I wasn't adhering to React best practices, so I did some refactoring and it worked without the full URL. Thanks for your help!Washrag
L
0

The error will also occur if you don't have the protocol in your request URL.

Instead

request.get('www.myexample.com/api/getStudents')

do

request.get('https://www.myexample.com/api/getStudents')
             ^^^^^^^^
Lobachevsky answered 4/1, 2019 at 17:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.