Node.js Heartbeat - Code Examples
Monitor your Node.js scripts and jobs with MoniTao heartbeat.
Node.js is widely used for async jobs, workers, and processing scripts. MoniTao heartbeat integration is simple with standard HTTP libraries.
Here are examples for different contexts: fetch API, axios, and native https module.
Modern method: fetch API
Available natively since Node.js 18+:
await fetch('https://monitao.com/api/heartbeat/ping/YOUR_TOKEN', {\n method: 'POST'\n});
With Axios
If you're already using axios in your project:
const axios = require('axios');\n\nawait axios.post('https://monitao.com/api/heartbeat/ping/YOUR_TOKEN');
seo.heartbeat_nodejs.cron_title
seo.heartbeat_nodejs.cron_intro
seo.heartbeat_nodejs.cron_example
FAQ - Node.js Heartbeat
How do I integrate with a Bull/BullMQ worker?
Add the ping at the end of your processor. Bull also lets you use completed events to trigger the ping.
Should the ping be awaited?
Not necessarily. If you want the script to terminate after the ping is confirmed, use await. Otherwise, fire-and-forget.
How do I handle ping errors?
Wrap in a try/catch. If the ping fails (MoniTao down, network), log but don't crash your job. It's monitoring, not a critical dependency.
Can I send metrics with the ping?
Yes, send a JSON body with duration, status, and other metrics. MoniTao records them for history.
Ready to Sleep Soundly?
Start free, no credit card required.