I see many applications that uses node runs forever.
Therefore, I tried using setInterval method, which I assumed it will let it run forever, but apparently it doesn’t.
var request = require('request');
var queue = function(item) {
request({
uri: 'http://www.google.net'
},
function(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Print the google web page.
}
})
};
setInterval(queue("google"),1000); //do this every 1 second.
When I run above program, it stops after a second.
How can I can modify above codes to keep it running if I run it with node?
Actually you have a bug in your code:
setInterval(queue("google"),1000); //do this every 1 second.Instead of passing a function above as the first parameter, you are passing the result of executing a function.
So either do
setInterval(queue, 1000)or do the following if you want multiple params: