How to Merge Text Files in Node.js
If you’re processing large volumes of text data, programmatically merging files can make your workflow much more efficient. The API solution provided below will integrate smoothly into your Node.js applications, automatically combining two text files into a single file, with the first input file stacking vertically on top of the second. It’s free and easy to use — just follow brief instructions below and grab a free-tier API key from our website to call the service (free-tier keys provide a limit of 800 API calls per month and no commitments).
To install the Node.js SDK, let’s copy and paste the below command and run it:
npm install cloudmersive-convert-api-client --save
Alternatively, we can add this snippet to our package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
For our final step, let’s call the function, including our free-tier API key (and eventually, our text inputs):
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.MergeDocumentApi();
var inputFile1 = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | First input file to perform the operation on.
var inputFile2 = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Second input file to perform the operation on (more than 2 can be supplied).
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.mergeDocumentTxt(inputFile1, inputFile2, callback);
Now you’re all done — you can combine your text data files with ease.