Home

multer

Multer is a Node.js middleware designed to handle multipart/form-data, primarily used for uploading files in web applications, especially with Express. It streamlines the process of parsing incoming form data and provides convenient access to uploaded files, while delegating the actual storage to configurable engines. Multer is widely used to add file upload functionality to server routes.

Multer offers storage engines and configuration options that control how and where files are saved. The diskStorage

The primary API involves creating a Multer instance with options and then attaching specific upload handlers

After processing, uploaded files are available on req.file for single-file uploads or req.files for multiple files,

engine
lets
you
customize
the
destination
directory
and
the
filename
for
saved
files,
enabling
predictable
paths
and
names.
The
memoryStorage
engine
keeps
uploaded
files
in
memory
as
Buffer
objects
for
immediate
processing
without
writing
to
disk.
In
addition
to
storage,
Multer
supports
a
fileFilter
function
to
accept
or
reject
files
by
type
and
a
limits
option
to
enforce
constraints
such
as
maximum
file
size
and
number
of
files.
The
middleware
relies
on
the
busboy
library
to
parse
multipart/form-data.
to
routes.
Typical
methods
bound
to
the
instance
include
single(fieldname)
for
a
single
file,
array(fieldname,
maxCount)
for
multiple
files
with
a
field,
fields(fieldsArray)
for
a
mix
of
named
fields,
and
any()
for
arbitrary
files.
Example
usage:
const
upload
=
multer({
storage:
diskStorage,
limits:
{
fileSize:
1024
*
1024
*
5
}
});
app.post('/upload',
upload.single('file'),
(req,
res)
=>
{
//
access
via
req.file
and
req.body
});
with
properties
such
as
originalname,
mimetype,
size,
and
either
path
(on
disk)
or
buffer
(in
memory).
Non-file
form
fields
populate
req.body.
Multer
is
open
source
and
distributed
via
npm,
commonly
used
to
implement
secure
and
controlled
file
upload
workflows
in
Node.js
applications.