How to scan a file for viruses in Node.js
1 min readMay 18, 2019
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.