Node Uptimer
Support: discord.gg/KRtQFb5tQh
NPM: npmjs.com/node-uptimer
Replit: replit.com/@m3ath/node-uptimer
This package provides a simple and flexible way to monitor the uptime of a website or API endpoint. It allows you to start and stop the monitoring process, as well as edit the monitoring interval.
Installation
Click here
npm install node-uptimer@latest
Usage
First, import the Uptime class from the package:
const { Uptime } = require('node-uptimer');
Creating an Uptime Instance
To create an instance of the Uptime
class, you need to provide the URL of the website or API endpoint you want to monitor, and an optional interval (in milliseconds) at which the monitoring should occur:
const uptime = new Uptime({ url: 'https://node-uptimer.m3ath.repl.co/', interval: 10000 });
Stopping and Starting Uptime Monitoring
To stop the uptime monitoring, call the stop()
method:
uptime.stop()
.then(() => {
console.log('Uptime monitoring stopped');
})
.catch((error) => {
console.error('Failed to stop uptime monitoring:', error.message);
});
You can re-start the uptime monitoring by calling the start()
method:
uptime.start()
.then(() => {
console.log('Uptime monitoring started');
})
.catch((error) => {
console.error('Failed to start uptime monitoring:', error.message);
});
Editing the Monitoring Interval
You can edit the monitoring interval by calling the edit()
method and providing a new interval (in milliseconds):
uptime.edit({ interval: 60000 })
.then(() => {
console.log('Monitoring interval updated');
})
.catch((error) => {
console.error('Failed to update monitoring interval:', error.message);
});
Events
The Uptime
class emits three events:
-
statusUpdate
: Triggered when the status of the monitored website changes. The event handler receives the status code (0 for stopped, 1 for started). -
intervalUpdate
: Triggered when the monitoring interval is updated. The event handler receives the old interval and the new interval. -
uptimeCreate
: Triggered when gets(fetchs) the url. The event handler receives the uptime object (createdTimestamp, status, interval, url).
const { Events, StatusType } = require('node-uptimer');
//statusUpdate
uptime.on(Events.StatusUpdate, (statusCode) => {
console.log('Status update:', statusCode + ` (${StatusType[statusCode]})`);
});
//intervalUpdate
uptime.on(Events.IntervalUpdate, (oldInterval, newInterval) => {
console.log('Interval update:', oldInterval, '->', newInterval);
});
//uptimeCreate
uptime.on(Events.UptimeCreate, (up) => {
console.log(`Uptimed url: ${up.url}, at: ${new Date(up.createdTimestamp).toLocaleString()}, status: ${up.status ? 'Success' : 'Failed'}, interval: ${up.interval}ms.`);
});