2

At one point I just want to make a POST request to my rails server running on localhost:3000

If a use like postman on even cUrl I can ping my API

But kow, with my node app I want to do this :

    var req_body = {
       'my_id': id,
        'dataTable': data }


    var options = {
       method:   'POST',
       url:      config.get('api_url') + 'end_tracking',
       json:     true,
       body:     req_body
      }
      //  config.get('api_url') return http://localhost:3000/

      request(options, function (error, response, body) {
                console.log(error)
    ... }

this is what I get :

{ Error: connect ECONNREFUSED 127.0.0.1:3000
at Object.exports._errnoException (util.js:896:11)
at exports._exceptionWithHostPort (util.js:919:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1073:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3000 }

and as I said, if I do this request directly with postman, everything works !

F4Ke
  • 1,631
  • 1
  • 20
  • 49

2 Answers2

2

I finally did it !

For a unknown reason, accessing localhost:3000, launched with rails s didn't work at all on my node server

but I worked when I started the rails server with rails s -b 0.0.0.0 !

F4Ke
  • 1,631
  • 1
  • 20
  • 49
  • amazing solution to a problem i was spending hours trying to fix. thanks. one question though, this strategy caused my Mac OS to ask for permission for ruby to accept incoming network connections. is this safe? what security considerations does this option entail? and is there documentation somewhere on the `-b` option? It doesn't seem to be in the build-in rails help menu. – s2t2 Dec 27 '17 at 19:51
0

how i did this , just use the same structure and i hope it works for you

request({
        url: url, //URL to hit
        method: 'post',
        headers: headers,
        timeout: 10000,
        body: JSON.stringify(body)
    }, function (error, result, body) {
        if (error) {
           console.log(error);
        } else if (result.statusCode == 500) {
            console.log('not found');
        } else {
            console.log('success')
        }
    });

hope it works.

Shekhar Tyagi
  • 1,644
  • 13
  • 18
  • same thing :( if I try to get localhost alone (without :3000) it shows the basic 'it works' But If I do my request with curl it succeded, why not on my node app – F4Ke Feb 10 '17 at 09:47