Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setInterval in nodejs

I am making an http request which should run after every one minute. Below is my code

var express = require("express");
var app = express();
var recursive = function () {
    app.get('/', function (req, res) {
        console.log(req);
        //Some other function call in callabck
        res.send('hello world');
    });
    app.listen(8000);
    setTimeout(recursive, 100000);
}
recursive();

According to the above code, I must get response after every one minute. But I am getting Error: listen EADDRINUSE. Any help on this will be really helpful.

like image 663
user134414214 Avatar asked Dec 14 '22 18:12

user134414214


1 Answers

This code makes http requests every minute:

var http = require('http');

var options = {
  host: 'example.com',
  port: 80,
  path: '/'
};
function request() {
  http.get(options, function(res){
    res.on('data', function(chunk){
       console.log(chunk);
    });
  }).on("error", function(e){
    console.log("Got error: " + e.message);
  });
}
setInterval(request, 60000);
like image 159
vp_arth Avatar answered Dec 31 '22 13:12

vp_arth