- 1 year ago
Checking the Online Statuses of Multiple Websites Simultaneously
To check the online status of multiple websites simultaneously in a script, you can use the http
or https
module in Node.js to send a request to each website and check the response status code.
Here's an example of how to check the online status of multiple websites in Node.js:
jsconst https = require('https');
const websites = ['https://www.google.com', 'https://www.facebook.com', 'https://www.twitter.com'];
websites.forEach(website => {
https.get(website, (res) => {
console.log(`${website} is ${res.statusCode === 200 ? 'online' : 'offline'}`);
}).on('error', (e) => {
console.log(`${website} is offline (${e.message})`);
});
});
In this example, we define an array of websites to check and use the forEach
method to iterate over each website. For each website, we send an HTTPS request using the https.get
method, and check the response status code to determine if the website is online or offline. If the website is online, we log a message indicating that it is online, and if it is offline (i.e., the request returns an error), we log a message indicating that it is offline and the error message.
Note that you can modify this script to check websites using HTTP instead of HTTPS by using the http
module instead of the https
module. You can also modify the timeout settings and error handling to suit your needs. Additionally, keep in mind that sending frequent requests to websites can cause network traffic and may be subject to rate limiting or other restrictions.