A complete example demonstrating how to integrate the attachmentAV API minto an Express.js application to scan file uploads for malware.
This example shows how to scan uploaded files for viruses using the attachmentAV API before processing them, supporting both single and multiple file uploads.
- Single file upload with malware scanning
- Multiple file upload with concurrent malware scanning
- Rate limiting for API calls to avoid throttling
- In-memory file processing with Multer
- Clean, interactive web interface with real-time feedback
- Node.js 18+ (for native fetch API support)
- An attachmentAV API key (Sign up here)
- Clone this repository:
git clone https://github.com/widdix/attachmentav-example-nodejs-express-middleware.git
cd attachmentav-example-nodejs-express-middleware.- Install dependencies:
npm install- Start the server:
ATTACHMENTAV_API_KEY="your-api-key-here" npm run serve- Open your browser and navigate to:
http://localhost:3000
- Upload a file using either form:
- Single file upload: Select one file and click "Upload and Scan"
- Multiple files upload: Select multiple files and click "Upload and Scan"
Code: https://github.com/widdix/attachmentav-example-nodejs-express-middleware/blob/main/index.js#L38-L67
- User selects a file through the web interface
- Multer middleware processes the multipart/form-data request
- File is stored in memory (not on disk)
- File buffer is sent to attachmentAV API for scanning
- Response handling:
- If malware detected → Returns 400 error with details
- If scan fails → Returns 500 error
- If clean → Returns 200 success with scan results
Code: https://github.com/widdix/attachmentav-example-nodejs-express-middleware/blob/main/index.js#L69-L102
- User selects multiple files
- Files are processed concurrently with rate limiting (max 3 concurrent requests)
- Each file is scanned independently
- If any file contains malware → Entire upload is rejected
- All files must be clean for success response
The example uses p-limit to limit concurrent attachmentAV API requests to 3, preventing API throttling when uploading multiple files.
Uploads and scans a single file.
Request:
- Content-Type:
multipart/form-data - Field name:
file
Success Response (200):
{
"message": "File uploaded and scanned successfully",
"filename": "example.pdf",
"scan": {
"status": "clean",
"hash": "...",
"scan_time": 0.123
}
}Error Response (400 - Malware Detected):
{
"error": "Malware detected",
"details": {
"status": "infected",
"viruses": ["EICAR-Test-File"],
"hash": "..."
}
}Uploads and scans multiple files.
Request:
- Content-Type:
multipart/form-data - Field name:
files(multiple files)
Success Response (200):
{
"message": "Files uploaded and scanned successfully"
}Error Response (400 - Malware Detected):
{
"error": "Malware detected",
"details": {
"status": "infected",
"viruses": ["EICAR-Test-File"],
"hash": "..."
}
}The example uses the EU endpoint by default:
const ATTACHMENTAV_URL = 'https://eu.developer.attachmentav.com/v1/scan/sync/binary';Available regions:
- EU:
https://eu.developer.attachmentav.com/v1/scan/sync/binary - US:
https://us.developer.attachmentav.com/v1/scan/sync/binary - Canada:
https://canada.developer.attachmentav.com/v1/scan/sync/binary - India:
https://india.developer.attachmentav.com/v1/scan/sync/binary
Change the ATTACHMENTAV_URL constant in index.js to use a different region.
attachmentAV allows files up to 10MB for the /v1/scan/sync/binary endpoint.
You can configure a custom file size limit when initializing multer in index.js:
const upload = multer({
storage: multer.memoryStorage(),
limits: {
fileSize: 10 * 1024 * 1024 // 10MB limit
}
});If you need to scan files larger than 10MB, consider using a different endpoint:
/v1/scan/sync/downloadfor scanning files up to 200MB from a URL -> requires additional setup to provide the uploaded file to attachmentAV, e.g. using a presigned URL/v1/scan/async/downloadfor scanning files up to 5GB in the background -> requires additional setup to process the callback response
Adjust the concurrent scan limit in the multi-upload endpoint:
const limit = pLimit(3); // Change to desired concurrency- File Size Limits: Consider adding file size limits to prevent memory exhaustion
- File Type Validation: Add file type validation based on your use case
- Rate Limiting: Implement rate limiting for uploads to prevent abuse
- Authentication: Add authentication before allowing file uploads
- HTTPS: Use HTTPS in production to protect API keys and data in transit
- Error Information: Limit error details exposed to clients in production
.
├── index.js # Express server with upload endpoints
├── package.json # Dependencies and scripts
├── public/
│ └── index.html # Upload interface with JavaScript
└── README.md # This file
- express (^5.2.1): Web framework
- multer (^2.0.2): Multipart/form-data handling for file uploads
- p-limit (^7.2.0): Rate limiting for concurrent API requests
To test malware detection, you can use the EICAR test file, which is a standard test file recognized by all antivirus software.
MIT

