Loading content…
Loading content…
Create a type-safe file validation function on both client (React forms) and server (Express endpoints). Validate that uploads are exclusively PDFs or PNG images, and restrict maximum file sizes to 5MB.
Expected Output
A client validation helper and a server validation middleware rejecting files that exceed size limits.
Click to toggle the recommended code solution
Architectural Explanation
The module configures Multer middlewares on Express. It restricts file sizes and validates MIME properties, rejecting unsupported formats.Code Snippet
// Server-side: Express Multer validator configuration
import multer from "multer";
const MAX_SIZE = 5 * 1024 * 1024; // 5MB
export const uploadConfig = multer({
limits: { fileSize: MAX_SIZE },
fileFilter: (req, file, cb) => {
const allowedMimeTypes = ["application/pdf", "image/png"];
if (allowedMimeTypes.includes(file.mimetype)) {
cb(null, true);
} else {
cb(new Error("Only PDF and PNG files are supported."));
}
}
});How to pitch this solution during technical interviews
I secure upload pipelines by validating files on both the client and the server. I check size and MIME parameters in React forms for quick user feedback, and enforce matching filters in Express upload middlewares.