How to convert website URL page to text TXT in Node.JS

Our goal in today’s tutorial is to use Node.JS to gather text from a URL and return it to us as a TXT file. This is actually a lot easier than you might think. Our secret weapon comes in the form of an API designed for the task.

To start things off, we shall add a reference to our package.json file that will be responsible for installing our API client.

"dependencies": {
"cloudmersive-convert-api-client": "^2.1.6"
}

Right after this, we can continue by calling this function:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');var defaultClient = CloudmersiveConvertApiClient.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 CloudmersiveConvertApiClient.ConvertWebApi();var input = new CloudmersiveConvertApiClient.UrlToTextRequest(); // UrlToTextRequest | HTML to Text request parametersvar callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.convertWebUrlToTxt(input, callback);

Now we can input any URL we wish into convertWebUrlToTxt and we will be given back our text. Easy.

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