How to remove HTML from a text string in Node.JS

The danger posed by cross-site-scripting and HTML attacks require proper security measures if you are to keep your delicate systems safe. The first line of defense should be filtering out potential HTML scripts from any incoming text submissions. We are going to look at a very simple yet effective method of doing this using Node.JS, with help from an API.

First we need to install our API’s client using npm install:

npm install cloudmersive-convert-api-client --save

Next we will call editTextRemoveHtml and input a text string to test out.

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.EditTextApi();var request = new CloudmersiveConvertApiClient.RemoveHtmlFromTextRequest(); // RemoveHtmlFromTextRequest | Input requestvar callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.editTextRemoveHtml(request, callback);

Following that, the API will process the text and send back the sanitized string. It’s as simple as that.

Image for post
Image for post

Written by

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

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