Validate url using NodeJS
For URL validations, you might not need a third party package always. You can use built-in URL package inside NodeJS or a URL constructor in the Browser. The following code works in a web browser.
To make the code work in NodeJS, you need to require the URL module in NodeJS.
const validateURL = url => {
try {
new URL(url);
return true;
} catch {
return false;
}
};
The following are some examples using above function.
validateURL('awsm');
// false
validateURL('awsm.page');
// false
validateURL('http:awsm.page');
// true
validateURL('https://awsm.page');
// true
Last Updated on
Comments