Ok, as a complete newbie to node.js I hoep I don't sound to ignorant. I am trying to write a very simple app to monitor a REST endpoint by issuing a GET and, assuming it gets a valid response, waits a given amount of time then runs again.
I have no problem with the requests and can run this with a cron job if needed. However, I'd like to just run this as a daemon. Is there a way to create a server like this in node maybe with an infinite loop that only exits on error and a timeout? Or even better, something cleaner?
Thanks in advance.
You can use setInterval and run the code for monitoring the REST service in the passed closure:
setInterval(function () {
// TODO: place the code for monitoring the REST service here
}, 60000); // change the interval to your liking.
You can also use a package called forever to make sure your script runs continuously.
In order to run your script as a daemon, you can (optionally) add the node shebang (#!/usr/bin/env node) and write an init script to run it:
#!/bin/sh
runOnBackground() {
# You may want to save the output into a log file instead of redirecting it to /dev/null
nohup $* > /dev/null 2>&1 &
}
# in your init script, when you want to start the script
runOnBackground /path/to/your/node/script.js