URL Validator Kit
A Node.js module to check whether a string is a valid URL.
Installation
Install the package via npm:
npm install check-valid-url --save
Usage
Import the module into your project:
const { isUrl } = require('check-valid-url');
const url = 'https://www.example.com';
const isValidUrl = isUrl(url);
if (isValidUrl) {
console.log('Valid URL');
} else {
console.log('Not a valid URL');
}
The isUrl
function returns true
if the provided string is a valid URL; otherwise, it returns false
.
API
-
isUrl(text)
Checks whether the provided string text
is a valid URL.
text
: The string to be checked.
Returns true
if text
is a valid URL; otherwise, returns false
.
-
isValidUrl(url, callback)
:
Example
isUrl
: Check if a string is a valid URL.
const { isUrl } = require('check-valid-url');
const url = 'https://www.example.com';
const isValidUrl = isUrl(url);
console.log(isValidUrl);
isValidUrl
: Check if a URL responds with a successful status code (2xx or 3xx).
const { isValidUrl } = require('check-valid-url');
const url = 'https://www.example.com';
isValidUrl(url, (err, isValid) => {
if (err) {
console.error(err);
} else {
console.log(isValid);
}
});
Testing
To run the tests, use:
npm test