Bhupi
Bhupi

Reputation: 2969

nodejs get request callback hitting multiple times

I am facing a problem, my get request's handler calling multiple times after a certain interval.

In the following example, if I call http://localhost:3000/test then it works perfectly fine but if I call http://localhost:3000 then it calls multiple times after a certain interval time. Someone please help me to understand what can be the issue with the task which takes long time and 'get' request.

var express = require('express');
var url = require('url');
var http = require('http');

var app = express();
var server = http.createServer(app);
app.listen(3000);
console.log('App is listening on port: 3000');

app.get('/', function(req, res){
    console.log("req url = "+ req.url);

    setTimeout(function(){
        console.log('working in settimeout');
        res.writeHead(200, {'content-type': 'text/plain'});
        res.end('This is simple response.');
    }, 1000*15*60);

    console.log('set timeout registered.');
});

app.get('/test', function(req, res){
    console.log("req url = "+ req.url);
    res.writeHead(200, {'content-type': 'text/plain'});
    res.end('This is response for /test.');
});

Please help.

Thanks.

Upvotes: 0

Views: 1465

Answers (1)

DevAlien
DevAlien

Reputation: 2476

I have tested it with chrome and now I understand what you mean. The browser, after waiting 120 seconds closes the connection and tries again.

This is why you see it multiple times, the request never finishes, since it is 900 seconds.

If you try with curlor something similar it will wait 900 seconds and you will see just one call.

Upvotes: 2

Related Questions