i new nodejs (and js), can explain me (or give link) how write simple service o nodejs, wich run permanently?
i want write service, wich send request every second foreign api @ store results db.
so, maybe nodejs have simple module run js method (methods) on , on again?
or, have write while
loop , there?
setinterval()
use run every second in node.js. call callback every nn milliseconds pass value of nn.
var interval = setinterval(function() { // execute request here }, 1000);
if want run forever, need handle situation remote server contacting off-line or having issues , not responsive normal (for example, may take more second troublesome request timeout or fail). might safer repeatedly use settimeout()
schedule next request 1 second after 1 finishes processing.
function runrequest() { issuerequest(..., function(err, data) { // process request results here // schedule next request settimeout(runrequest, 1000); }) } // start repeated requests runrequest();
in code block issuerequest()
placeholder whatever networking operation doing (i use request()
module , use request.get()
.
because request processing asynchronous, not recursion , not cause stack buildup.
fyi, node.js process active incoming server or active timer or in-process networking operation not exit (it keep running) long have timer running or network request running, node.js process keep running.
Comments
Post a Comment