aditya_medhe
aditya_medhe

Reputation: 372

Receiving 2 HTTP requests on the server when only 1 sent

I am creating an app and using http://c9.io environment to develop it. It is a NodeJS app, which provides some REST endpoints for the client side application to query. Till now, everything was running fine, and today what I observe is that for 1 call sent by the browser to the REST API, 2 requests are being shown as received, and the request handler is being called 2 times. This has slowed the response time for one request.

In Chrome developer tools, it shows only one request sent, however, I am using app.use() to log incoming requests in Express and it prints the same 2 times for each request. Also, the handler is called twice.

This is happening intermittently, not every time. I am behind a corporate network. As I have sent a lot of requests in the day for testing, is there any chance that a monitoring program is sending the requests since it finds it suspicious? I have not edited the code that handles the requests.

Edit: Adding the code for handlers as suggested.

app.get('/suggestions/:keyword', function(r, s) {
    sug_db.retrieveSuggestions(r.params.keyword, function(data) {
        s.writeHead(200, {'content-type': 'text/html'});
        s.write(renderSugg({data: data}))
        s.end();
    });
});

app.get('/search/:query', function(r, s) {
    esc_db.search(r.params.query, function(data) {
        s.send(renderResults({query: r.params.query, results:data}));
    });
});

As you can see, they do nothing but get some data from a database and return the result as HTTP response. The templating engine I am using is Pug (formerly Jade)

Upvotes: 1

Views: 2619

Answers (1)

rsp
rsp

Reputation: 111466

It doesn't look like that code that you included in the question can be guilty of running twice. But maybe some code in sug_db.retrieveSuggestions or esc_db.search does that.

What I would do is this:

Add some logging inside the code that you provided, both before calling the functions and inside the callback:

app.get('/suggestions/:keyword', function(r, s) {
    console.log('*** GET /suggestions/:keyword handler');
    sug_db.retrieveSuggestions(r.params.keyword, function(data) {
        console.log('GET /suggestions/:keyword callback');
        s.writeHead(200, {'content-type': 'text/html'});
        s.write(renderSugg({data: data}))
        s.end();
    });
});

app.get('/search/:query', function(r, s) {
    console.log('*** GET /search/:query handler');
    esc_db.search(r.params.query, function(data) {
        console.log('GET /search/:query callback');
        s.send(renderResults({query: r.params.query, results:data}));
    });
});

(or change console.log to whatever method of logging you use). I would see what is actually called twice - the handlers themselves, or the callbacks, or none. Next would be examination of the functions that are actually called by the handlers:

  • sug_db.retrieveSuggestions()
  • esc_db.search()
  • renderSugg()
  • renderResults()

It's important to see what is actually called twice and then examine why it can be happening. But it can happen if, for example, you do something like:

function badFunction(data, callback) {
  if (something) {
    callback('error');
  }
  callback('ok');
}

instead of:

function goodFunction(data, callback) {
  if (something) {
    callback('error');
  } else {
    callback('ok');
  }
}

I would expect that the functions that are called from the handlers could do something like that to call the callback twice - and maybe the condition or error that they checking didn't happen before but happens now, causing the change in behavior.

Upvotes: 1

Related Questions