Socket
Socket
Sign inDemoInstall

multer

Package Overview
Dependencies
Maintainers
3
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

multer

Middleware for handling `multipart/form-data`.


Version published
Weekly downloads
5.2M
increased by1.48%
Maintainers
3
Weekly downloads
 
Created

What is multer?

Multer is a node.js middleware for handling multipart/form-data, which is primarily used for uploading files. It is written on top of busboy for maximum efficiency.

What are multer's main functionalities?

File Uploads

This feature allows you to upload files to your server. The code sample demonstrates how to handle a single file upload with Multer.

const multer = require('multer');
const upload = multer({ dest: 'uploads/' });

app.post('/upload', upload.single('file'), function (req, res) {
  // req.file is the `file` file
  res.send('File uploaded!');
});

Multiple Files Upload

Multer also supports uploading multiple files at once. The code sample shows how to handle multiple file uploads, limiting to 12 files in this case.

const multer = require('multer');
const upload = multer({ dest: 'uploads/' });

app.post('/upload', upload.array('files', 12), function (req, res) {
  // req.files is array of `files` files
  res.send('Multiple files uploaded!');
});

Disk Storage

Multer allows you to customize the storage of files. This code sample demonstrates how to use disk storage to control the storage location and file naming.

const multer = require('multer');

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'uploads/')
  },
  filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now())
  }
});

const upload = multer({ storage: storage });

Memory Storage

For temporary storage or when you want to process the file without saving it to disk, you can use memory storage. The code sample shows how to store a file in memory.

const multer = require('multer');
const upload = multer({ storage: multer.memoryStorage() });

app.post('/upload', upload.single('file'), function (req, res) {
  // req.file is the `file` file stored in memory
  res.send('File uploaded and stored in memory!');
});

File Filtering

Multer provides a way to filter out files based on conditions you set. This code sample demonstrates file filtering to only allow JPEG images.

const multer = require('multer');
const upload = multer({
  fileFilter: function (req, file, cb) {
    if (file.mimetype !== 'image/jpeg') {
      return cb(new Error('Only JPEG files are allowed!'), false);
    }
    cb(null, true);
  }
});

Other packages similar to multer

Keywords

FAQs

Package last updated on 19 Sep 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc