2015-09-18 67 views
0

我面临一个问题,我的获取请求的处理程序在特定时间间隔后多次调用。nodejs获取多次请求回调

在下面的例子中,如果我打电话http://localhost:3000/test那么它工作得很好,但如果我拨打http://localhost:3000,那么它在一定的时间间隔后会多次呼叫。有人请帮我理解什么是需要很长时间和'取得'请求的任务的问题。

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.'); 
}); 

请帮忙。

谢谢。

+0

Exactely发生了多次? – DevAlien

回答

2

我已经用铬进行了测试,现在我明白你的意思了。 浏览器在等待120秒后关闭连接并再次尝试。

这就是为什么你多次看到它,请求永远不会结束,因为它是900秒。

如果您尝试使用curl或类似的东西,它会等待900秒,您将看到只有一个电话。

+0

好吧,我想我明白了你的意思,但是有什么办法可以解决这个问题。 – Bhupi

+0

浏览器设置了120秒的限制,等待超过120加载网页是没有用的。 – DevAlien

+0

如果你做了很长的过程,你可以setTimeout false在快递 –