1.1.2 • Published 7 months ago
express-filer v1.1.2
📄 express-file
express-filer is a middleware for Node.js and Express applications that simplifies the handling of file uploads using the multer library.
🔌 Installation
npm install express-filer
⚡ Usage
import express from 'express';
import expressFiler from 'express-filer';
const api = express.Router();
api.post(
'/',
expressFiler.uploadFile('files', 3, (err, req, res, next) => {
if (err) {
if (
err instanceof multer.MulterError &&
err.code === 'LIMIT_UNEXPECTED_FILE'
) {
return res.status(400).send('Too many files uploaded.');
} else {
return res.status(500).send('Something went wrong.');
}
}
return next();
}),
(_, res) => {
console.log(req.files);
res.sendStatus(200);
},
);
export default api;
In this example, we're using express-filer to handle file uploads in an Express route. The uploadFile function takes three parameters:
fieldName
: The name of the field that contains the file(s) in the request.maxCount
: The maximum number of files that can be uploaded.callback
: A callback function.
All uploaded files are temporarily stored in memory as Buffer objects, avoiding the need for disk space allocation. The uploaded files can be accessed at req.files (in this example).
📖 License
This script is licensed under the MIT License.
Feel free to use, modify, and distribute this script as per the terms of the license.