How to Syntactically Validate a URL in Node.JS

Syntactic validation is the quick and easy method to weed out fake URLs. While it does not have the power of full validation (see the related article for more on that), it is certainly efficient. We are not going to go through the trouble of coding this one out. Instead, we will employ an API that is already set up to accomplish this very task, saving us a ton of time.

To use our API and its functions, we need to install its client, which can be done using this reference in your package.json.

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

Next, call the function ValidateUrlRequestSyntaxOnly, seen below.

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.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);

All you need to do now is enter your URL and it will return true or false for its syntax. No problem!

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