How to get document type information in Node.JS

Cloudmersive
2 min readApr 6, 2020

Sometimes file extensions get removed, and there is nothing more annoying than that. To deal with this problem, we need to retrieve document type information from our file. Setting this up in Node.JS is pretty annoying. That’s why we’ve already done it all for you. All you need to do is make use of an API.

We begin by adding this snippet to our package.json. Doing so will install our API client and allow us to call our function.

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

The function we will need is called convertDocumentAutodetectGetInfo, and we can implement it with the following lines of code.

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.ConvertDocumentApi();var inputFile = "/path/to/file"; // File | Input file to perform the operation on.var callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.convertDocumentAutodetectGetInfo(inputFile, callback);

Done! Now any file that we input will be identified, including files without extensions and those with the wrong extensions. Problem solved.

--

--

Cloudmersive

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