Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple responses on node.js restler call

I've created a library for sending REST requests:

var rest = require('restler');
module.exports = {
  beginSession: function()
  {
    var options = {
        method: "GET",
        query: {begin_session: '1'}};
    rest.get('http://countly/i', options).
        on('complete', function(data, response){
            console.log('Status: ' + response.statusCode);
        });
  }
};

The problem is that every time I use the library and the call is responded, the 'on complete' is called multiple times: 1st use of method will call 'on complete' just once, 2nd use of method will call 'on complete' twice and so on....

What Am I doing wrong?

Thanks Jose

like image 245
JoseOlcese Avatar asked Apr 16 '13 21:04

JoseOlcese


People also ask

What is the difference between response send() and response write() in express?

response. send() sends the response and closes the connection, whereas with response. write() you can send multiple responses. In this article, I will explain the difference between response.

Can we call API from node JS?

The simplest way to call an API from NodeJS server is using the Axios library. Project Setup: Create a NodeJS project and initialize it using the following command. Module Installation: Install the required modules i.e. ExpressJS and Axios using the following command.

What is request and response in node JS?

js Request and Response objects are the parameters of the callback function which is used in Express applications. The express. js request object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, and so on.


3 Answers

I was struggling with this one as well. but didn't find an answer on the internet. I finally figure it out though. It was caused by the 'complete' event being registered every time the your rest.get() is called.

My solution is to use .once() instead of .on(). For example:

var rest = require('restler');
rest.get('url_to_fetch').once('complete', function(rtn, rsp){
     ....blah blah....
});
// refer here http://nodejs.org/api/events.html#events_emitter_once_event_listener

Hopefully this helps.

like image 127
user2413287 Avatar answered Oct 07 '22 14:10

user2413287


TL;DR: Bug in restler, quick fix until npm is updated: add git master to package.json

The real problem here is that some changes to the event API in node 0.10 results in restler refiring old event listeners as described in https://github.com/danwrong/restler/issues/112.

End of august this was fixed in https://github.com/danwrong/restler/pull/113. While we wait for a proper npm release it works for me by using the current git head.

"restler": "git://github.com/danwrong/restler.git#9d455ff14c57ddbe263dbbcd0289d76413bfe07d"

DISCLAIMER: I don't know what is broken in this version or why it is not released yet. I did not go trough the issues or diffs since last release to find out.

UPDATE Aug 2014: There was a npm release since then, it seems to include the fix.

like image 44
beilharz Avatar answered Oct 07 '22 12:10

beilharz


This is because you attach a new event for each call. Try to unbind event first.

like image 37
Aleko Avatar answered Oct 07 '22 13:10

Aleko