file-mime-guard
v1.0.2
Published
A tiny utility to validate file MIME types in Node.js
Maintainers
Readme
file-mime-guard — Validate File MIME Types in Node.js (Upload Security)
Validate file MIME types in Node.js and JavaScript. Protect your backend from invalid or malicious uploads by checking file types before processing.
Perfect for upload validation, API security, and media pipelines.
Features
- Check whether a MIME type is allowed
- Validate uploaded files against an allowlist
- Simple and predictable API
- Lightweight helper for backend services
Install
npm install file-mime-guardUsage
import { isAllowedMimeType } from "file-mime-guard";
const allowed = isAllowedMimeType("audio/wav", [
"audio/wav",
"audio/mpeg",
"audio/webm",
]);
console.log(allowed); // trueAPI
isAllowedMimeType(mimeType: string, allowed: string[]): boolean
Returns true if the MIME type exists in the allowed list.
assertMimeType(mimeType: string, allowed: string[]): void
Throws an error if the MIME type is not allowed.
Example
import { assertMimeType } from "file-mime-guard";
assertMimeType("audio/mpeg", [
"audio/wav",
"audio/mpeg",
]);
console.log("File type is valid");Use Cases
- Validate file uploads in Node.js APIs (Express, Fastify)
- Prevent users from uploading unsupported or dangerous file types
- Restrict uploads to audio, image, or video formats
- Add security checks before storing files or processing them
- Protect transcription or media pipelines from invalid input
Why file-mime-guard?
Most file upload handlers trust user input — which can be unsafe.
This package provides a simple way to enforce allowed MIME types and prevent invalid or malicious uploads.
Related
Building voice apps or audio tools?
👉 https://www.vocallab.ai — generate voiceovers and captions from text
License
MIT
Keywords
- validate file upload nodejs
- check mime type javascript
- file upload security node js
- prevent invalid file upload
- mime type validation npm
