How to Append a Paragraph to an HTML Document in Node.js
Looking for an easy, programmatic way to make simple edits to HTML documents? A free API can do the trick.
Using the below code, we can take advantage of a free API that allows us to append paragraph elements to the bottom of our HTML documents. We can specify our paragraph text and CSS styling for our paragraph in our request, and we’ll receive the finalized HTML result in our API response.
In order to authorize our API requests for free, we’ll just need a free-tier API key. This can be obtained easily by registering a free account on the Cloudmersive website, and it’ll allow us to make up to 800 API calls per month with no commitment.
To begin structuring our API call, let’s start by installing the SDK. We can do that in one of two ways — either by running the below command:
npm install cloudmersive-convert-api-client --save
Or by adding this snippet to our package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
Lastly, we can use the below code to call the function, and we can customize request parameters accordingly:
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 paragraphText = "paragraphText_example"; // String | The text content to be used in the paragraph.
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.
'cssStyle': "cssStyle_example" // String | Optional: The CSS style for the paragraph.
};
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.editHtmlHtmlAppendParagraph(paragraphText, opts, callback);
Now we can easily add a critical HTML element to any HTML file without any extra hassle.