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
2 Answers
Reset to default 11This 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);
}