{"library":"multer","title":"Multer","description":"Multer is a Node.js middleware for handling `multipart/form-data`, primarily used for file uploads in web applications built with frameworks like Express. It is built on top of `busboy` for efficient streaming and processing of incoming form data. The current stable version is 2.1.1. Multer maintains an active release and security cadence, with multiple patches in recent minor versions (e.g., 2.0.1 through 2.1.1) addressing critical security vulnerabilities (CVEs). Its key differentiators include its robust handling of various file upload scenarios (single, array, multiple fields) and its straightforward API, making it a standard choice for file uploads in the Node.js ecosystem. It explicitly only processes `multipart/form-data` and ignores other content types.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install multer"],"cli":null},"imports":["import multer from 'multer';\n// or\nconst multer = require('multer');","import multer, { DiskStorageOptions, StorageEngine } from 'multer';\n\nconst storage = multer.diskStorage({\n  destination: (req, file, cb) => { /* ... */ },\n  filename: (req, file, cb) => { /* ... */ }\n});","import multer, { MulterError } from 'multer';\n\napp.post('/upload', upload.single('file'), (err, req, res, next) => {\n  if (err instanceof MulterError) { /* handle Multer-specific error */ }\n});"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import express from 'express';\nimport multer from 'multer';\nimport path from 'path';\nimport { fileURLToPath } from 'url';\nimport { dirname } from 'path';\nimport fs from 'fs';\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = dirname(__filename);\n\nconst app = express();\nconst PORT = process.env.PORT || 3000;\n\n// Ensure the uploads directory exists\nconst uploadsDir = path.join(__dirname, 'uploads');\nif (!fs.existsSync(uploadsDir)) {\n  fs.mkdirSync(uploadsDir);\n}\n\n// Configure disk storage for Multer\nconst storage = multer.diskStorage({\n  destination: (req, file, cb) => {\n    cb(null, uploadsDir);\n  },\n  filename: (req, file, cb) => {\n    cb(null, Date.now() + '-' + file.originalname);\n  }\n});\n\nconst upload = multer({ storage: storage, limits: { fileSize: 5 * 1024 * 1024 } }); // 5MB limit\n\n// Basic HTML form for upload\napp.get('/', (req, res) => {\n  res.send(`\n    <form action=\"/upload-profile\" method=\"post\" enctype=\"multipart/form-data\">\n      <input type=\"file\" name=\"avatar\" />\n      <button type=\"submit\">Upload Avatar</button>\n    </form>\n  `);\n});\n\n// Route to handle single file upload\napp.post('/upload-profile', upload.single('avatar'), (req, res) => {\n  if (!req.file) {\n    return res.status(400).send('No file uploaded.');\n  }\n  res.send(`File uploaded successfully: ${req.file.filename}`);\n});\n\n// Global error handler for Multer errors\napp.use((err, req, res, next) => {\n  if (err instanceof multer.MulterError) {\n    return res.status(500).send(`Multer error: ${err.message}`);\n  } else if (err) {\n    return res.status(500).send(`Unknown error: ${err.message}`);\n  }\n  next();\n});\n\napp.listen(PORT, () => {\n  console.log(`Server running on http://localhost:${PORT}`);\n});","lang":"javascript","description":"This quickstart demonstrates setting up an Express server to handle a single file upload using Multer's disk storage, including basic error handling and a 5MB file size limit. It configures the destination and filename for uploaded files.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}