Get a Canonical URL in Node.JS
If you are working with an HTML document and need to retrieve the canonical link, you don’t want to do a lot of comparison between documents and searching through HTML code to find it. By utilizing this simple API in Node.JS, you will be able to automatically get the rel=canonical URL of the HTML document — no hassle needed.
To kick off the operation, we can run this command to install the SDK:
npm install cloudmersive-convert-api-client --save
Or, we can add this snippet to our package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
With the installation out of the way, we can input our file or file URL and API key into the following code to call the function:
var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';var apiInstance = new CloudmersiveConvertApiClient.EditHtmlApi();var opts = {
'inputFile': Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer), // File | Optional: Input file to perform the operation on.
'inputFileUrl': "inputFileUrl_example" // String | Optional: URL of a file to operate on as input.
};var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.editHtmlHtmlGetRelCanonical(opts, callback);
To obtain your API key, visit the Cloudmersive website to register for a free account; this will provide 800 monthly calls across our multitude of APIs.