-4

I need to call an API continuously from server side itself, so that it is called 24/7 every second. How can I achieve this?

I tried it as shown below in server.js, but getting 'TypeError: request.put is not a function'.

app.get('/',function(request,response){
    setInterval(function(){
        request.put('http://localhost:4242/changepaidstatus', function(error,response){
            if (error){
                console.log(error);
            }
            else{
                console.log(response);
            }
        });
    }, 1000);
});
G.Mounika
  • 385
  • 1
  • 3
  • 16
  • Possible duplicate of [Calling a function every 60 seconds](http://stackoverflow.com/questions/3138756/calling-a-function-every-60-seconds) – Nicholas Smith Feb 04 '17 at 09:27

1 Answers1

1

setInterval() will let you repeat some function every second.

setInterval(() => {
    // will execute every second
}, 1000);

As for calling the API, you can use the request() module to make any http request you would like. Here's an example from their doc:

var request = require('request');
request('http://www.google.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body) // Show the HTML for the Google homepage. 
  }
})
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • getting 'TypeError: request.put is not a function'. – G.Mounika Feb 04 '17 at 11:16
  • @G.Mounika - I can't exactly help you with code I can't see. Did you properly install the request library using NPM? `request.put()` works just fine if you do things properly, so apparently your code (which I can't see) has an error in it. – jfriend00 Feb 04 '17 at 16:56