最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - setInterval in nodejs - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Nov 13, 2014 at 5:42 gp. 8,2253 gold badges42 silver badges41 bronze badges asked Nov 13, 2014 at 5:40 user134414214user134414214 731 gold badge1 silver badge7 bronze badges 7
  • 1 possible duplicate of nodejs Error: listen EADDRINUSE – Colin Brock Commented Nov 13, 2014 at 5:43
  • but here im using setInterval. Any idea on this? – user134414214 Commented Nov 13, 2014 at 5:44
  • your code starts a server at 8000 port and sets a handler for handling get request to '/'. You need to do this only once. Another point, you are not actually making a call to your server so no response every minute. – gp. Commented Nov 13, 2014 at 5:44
  • Thanks for quick reply. So how can I make it work? – user134414214 Commented Nov 13, 2014 at 5:49
  • 2 your task is completely incorrect. Think more about actual issue you try to solve. – vp_arth Commented Nov 13, 2014 at 5:58
 |  Show 2 more comments

2 Answers 2

Reset to default 11

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);

EADDRINUSE error is thrown because you can't start a server in the same port twice

It would work of the next way:

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

评论列表(0)

  1. 暂无评论