Below is a minimal example of making an HTTP request to Healthchecks from Node.js.
var https = require('https');
https.get('https://healthchecks.niyawe.de/ping/your-uuid-here').on('error', (err) => {
console.log('Ping failed: ' + err)
});
Note: the "https" library executes requests asynchronously. If you send both "start" and "success" signals, you can encounter a race condition where the "success" signal arrives before the "start" signal. Avoid the race condition by using callbacks, promises or the async/await feature. Here is an example that uses async/await and the axios library:
const axios = require("axios");
async function ping(url) {
try {
await axios.get(url, {timeout: 5000});
} catch(error) {
// Log the error and continue. A ping failure should
// not prevent the job from running.
console.error("Ping failed: " + error);
}
}
async function runJob() {
var pingUrl = "https://healthchecks.niyawe.de/ping/your-uuid-here";
await ping(pingUrl + "/start");
try {
console.log("TODO: run the job here");
await ping(pingUrl); // success
} catch(error) {
await ping(pingUrl + "/fail");
}
}
runJob();
You can also send pings from a browser environment. Healthchecks sets the
Access-Control-Allow-Origin:*
CORS header, so cross-domain AJAX requests work.
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://healthchecks.niyawe.de/ping/your-uuid-here', true);
xhr.send(null);