{"library":"multer-autoreap","title":"Multer AutoReap","description":"multer-autoreap is an Express middleware designed to automatically clean up temporary files uploaded to disk by `multer` or other `multipart/form-data` parsing middleware. Upon completion of an HTTP request (either success or error, configurable via `reapOnError`), the middleware identifies and deletes files referenced in `req.files` or `req.file` that were stored in temporary locations. The current stable version is 1.0.3, with releases being infrequent but focused on stability and compatibility fixes, particularly with newer Node.js versions and Multer core changes. Its primary differentiator is the immediate, request-lifecycle-driven cleanup, offering a more responsive alternative to age-based file reaping solutions and mitigating potential disk space exhaustion vulnerabilities inherent in handling file uploads.","language":"javascript","status":"active","last_verified":"Thu Apr 23","install":{"commands":["npm install multer-autoreap"],"cli":null},"imports":["const autoReap = require('multer-autoreap');","const autoReap = require('multer-autoreap');\nautoReap.options.reapOnError = true;"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const express = require('express');\nconst multer  = require('multer');\nconst autoReap  = require('multer-autoreap');\n\nconst app = express();\n\n// Configure Multer to save files to a temporary directory\nconst upload = multer({ dest: './tmp-uploads/' });\n\n// Apply multer-autoreap as a global middleware\napp.use(autoReap);\n\n// Example route for file upload\napp.post('/upload', upload.single('myFile'), function (req, res, next) {\n  res.on('autoreap', function(file) {\n    console.log('File automatically reaped:', file.filename, 'at', file.path);\n  });\n  if (!req.file) {\n    return res.status(400).send('No file uploaded.');\n  }\n  res.send(`File '${req.file.originalname}' uploaded and will be cleaned up.`);\n});\n\n// Start the server\nconst PORT = process.env.PORT || 3000;\napp.listen(PORT, () => {\n  console.log(`Server listening on port ${PORT}`);\n  console.log(`Temp upload directory: ${upload.dest}`);\n  console.log('Upload a file via POST to /upload with field name \"myFile\"');\n});","lang":"javascript","description":"This quickstart demonstrates setting up an Express server with Multer for file uploads and integrating `multer-autoreap` as global middleware to automatically clean up temporary files after the request finishes. It also shows how to listen for the `autoreap` event on the response object.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}