multer
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
append-field | 1.0.0 | 2.7 kB | MIT | prod | |
busboy | 1.6.0 | 19.67 kB | MIT | prod | |
concat-stream | 1.6.2 | 3.72 kB | MIT | prod | |
mkdirp | 0.5.6 | 2.95 kB | MIT | prod | |
object-assign | 4.1.1 | 2.61 kB | MIT | prod | |
type-is | 1.6.18 | 5.73 kB | MIT | prod | |
xtend | 4.0.2 | 2.47 kB | MIT | prod |
Multer is a Node.js middleware for handling multipart/form-data
, commonly used for uploading files. Written on top of busboy for optimum efficiency, Multer adds a body
object and a file
or files
object to the request
object. Please note that Multer will not process forms that are not multipart.
To use Multer, first, you need to install it using npm.
npm install --save multer
After installation, you can use Multer in your application. Below are some basic usage examples:
// Importing express and multer module
const express = require('express')
const multer = require('multer')
// Specify the destination for uploading files
const upload = multer({ dest: 'uploads/' })
// Creating an instance of express
const app = express()
// Handling routes
app.post('/profile', upload.single('avatar'), function (req, res, next) {
// req.file contains properties of 'avatar' file
// req.body contains properties of text fields, if there were any
})
app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
// req.files will contain all 'photos' files
// req.body will contain the text fields, if there were any
})
const cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
app.post('/cool-profile', cpUpload, function (req, res, next) {
// req.files is an object where the key is fieldname, and the value is an array of files
// e.g., req.files['avatar'][0] -> File, req.files['gallery'] -> Array
// req.body will contain the text fields, if there were any
})
In case you need to handle a text-only multipart form, you should use the .none()
method:
app.post('/profile', upload.none(), function (req, res, next) {
// req.body contains the text fields
})
The full multer documentation can be found on the official GitHub page of the Multer project. The documentation includes more detailed usage instructions, API reference, custom storage engine implementation, and error handling practices.