How to Validate a URL in Node.JS

To help keep your website safe and provide the best experience for your clients, validating any domain names that are associated with or linked to by your organization will ensure that no links are broken and prevent potential virus risks. This can also be helpful when using applications that require URL input from outside sources, as it will mitigate any cyber attacks through these means. The following two APIs will aid you in performing these checks; the first will simply check if a URL is syntactically correct and the second performs a full verification for syntax, endpoint existence, and virus scan. Both APIs accept various types of URL input and will produce a well-formed URL as output.

Image for post
Image for post

For both APIs, we will first run this command to install our client:

npm install cloudmersive-validate-api-client --save

You can also add this snippet to your package.json:

"dependencies": {
"cloudmersive-validate-api-client": "^1.2.4"
}

Then, for our DomainUrlSyntaxOnly API, we will call the following function:

var CloudmersiveValidateApiClient = require('cloudmersive-validate-api-client');
var defaultClient = CloudmersiveValidateApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveValidateApiClient.DomainApi();var request = new CloudmersiveValidateApiClient.ValidateUrlRequestSyntaxOnly(); // ValidateUrlRequestSyntaxOnly | Input URL informationvar callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.domainUrlSyntaxOnly(request, callback);

If you’d like a more in-depth scan, you can call the following function, DomainUrlFull:

var CloudmersiveValidateApiClient = require('cloudmersive-validate-api-client');
var defaultClient = CloudmersiveValidateApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveValidateApiClient.DomainApi();var request = new CloudmersiveValidateApiClient.ValidateUrlRequestFull(); // ValidateUrlRequestFull | Input URL requestvar callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.domainUrlFull(request, callback);

With these, you can easily mitigate any cyber threats while ensuring that any URL you use meets syntax criteria and validity. You can retrieve your free API Key from the Cloudmersive website; this allows up to 800 calls across our library of APIs.

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store