How to scan a file for viruses in Node.js

First, we need to add a reference to the package.json:

"dependencies": {
"cloudmersive-virus-api-client": "^1.1.2"
}

Then, we need to require the library:

var CloudmersiveVirusApiClient = require('cloudmersive-virus-api-client');

Finally we need to call the scanFile function with the file buffer or byte array that we want to scan for viruses:

var defaultClient = CloudmersiveVirusApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = "YOUR API KEY"

var api = new CloudmersiveVirusApiClient.ScanApi()

var inputFile = fs.readFileSync("C:\\temp\\input.docx");


var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
api.scanFile(Buffer.from(inputFile.buffer), callback);

That’s all we need to do! Now the file will be scanned and the result printed to the screen. If a threat is found, the identify of the threat is also included in the response.

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