How to fully validate a URL in Node.JS

URL validation can be used for all sorts of excellent applications, such as guarding against broken links or to aid in filtering spam user submissions. Regardless of how you will use it, we are going to dive into how to perform this task using Node.JS.

Begin by using npm install on our API client.

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

With that out of the way, proceed with calling the URL validation function from the client:

var CloudmersiveValidateApiClient = require('cloudmersive-validate-api-client');var defaultClient = CloudmersiveValidateApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';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);

All that is left to do now is enter your URL and test it out. This API will not only check the syntax of the URL, but also check that it exists, check the endpoint, and scan for viruses. All in all, a great way to vet your links!

Image for post
Image for post

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