Introduction to is-reachable
The is-reachable
package is an efficient tool for network reachability checks. It helps developers quickly determine if a host or service is reachable, making it an essential utility for network-related applications.
Basic Usage
To get started with is-reachable
, you first need to install it using npm:
npm install is-reachable
Once installed, you can use it in your JavaScript or TypeScript projects:
const isReachable = require('is-reachable');
(async () => {
console.log(await isReachable('google.com')); // true
})();
Advanced Usage
You can specify different schemes to check various services such as HTTP, HTTPS, FTP, and more:
(async () => {
console.log(await isReachable('http://google.com')); // true
console.log(await isReachable('https://google.com')); // true
console.log(await isReachable('ftp://example.com')); // false
})();
Error Handling
Handling errors and exceptions is crucial for robust applications. Here is how you can handle errors with is-reachable
:
(async () => {
try {
const reachable = await isReachable('invalid-url');
console.log(reachable);
} catch (error) {
console.error('Error:', error);
}
})();
App Example
Below is a simple app example that checks the reachability of several services and logs the results:
const isReachable = require('is-reachable');
(async () => {
const services = [
'http://google.com',
'https://github.com',
'ftp://example.com'
];
for (const service of services) {
const reachable = await isReachable(service);
console.log(`${service} is ${reachable ? 'reachable' : 'not reachable'}`);
}
})();