Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
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.
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);
}
});
Formidable is an alternative to Multer for parsing form data, especially file uploads. It is less middleware-oriented and more flexible in terms of handling various form parsing tasks.
Busboy is a low-level Node.js module for parsing incoming HTML form data. Multer is built on top of Busboy, but provides a more convenient middleware API for integrating with Express.js applications.
Multiparty is another module for handling multipart/form-data requests, which is the type of requests that file uploads usually come in. It is similar to Multer but has a different API and is used in a slightly different way.
Multer is a node.js middleware for handling multipart/form-data
.
It is written on top of busboy for maximum efficiency.
$ npm install multer
var express = require('express')
var multer = require('multer')
var app = express()
app.use(multer({ dest: './uploads/'}))
You can access the fields and files in the request
object:
console.log(req.body)
console.log(req.files)
IMPORTANT: Multer will not process any form which is not multipart/form-data
submitted via the POST
or PUT
methods.
A multer file object is a JSON object with the following properties.
fieldname
- Field name specified in the formoriginalname
- Name of the file on the user's computername
- Renamed file nameencoding
- Encoding type of the filemimetype
- Mime type of the filepath
- Location of the uploaded fileextension
- Extension of the filesize
- Size of the file in bytestruncated
- If the file was truncated due to size limitationMulter accepts an options object, the most basic of which is the dest
property, which tells Multer where to upload the files. In case you omit the options object, the file will be renamed and uploaded to the temporary directory of the system.
By the default, Multer will rename the files so as to avoid name conflicts. The renaming function can be customized according to your needs.
The following are the options that can be passed to Multer.
dest
limits
rename(fieldname, filename)
onFileUploadStart(file)
onFileUploadData(file, data)
onFileUploadComplete(file)
onParseStart()
onParseEnd(req, next)
onError()
onFilesLimit()
onFieldsLimit()
onPartsLimit()
Apart from these, Multer also supports more advanced busboy options like highWaterMark
, fileHwm
, and defCharset
.
In an average web app, only dest
and rename
might be required, and configured as shown in the example.
app.use(multer({
dest: './uploads/',
rename: function (fieldname, filename) {
return filename.replace(/\W+/g, '-').toLowerCase() + Date.now()
}
}))
The details of the properties of the options object is explained in the following sections.
The destination directory for the uploaded files.
dest: './uploads/'
An object specifying the size limits of the following optional properties. This object is passed to busboy directly, and the details of properties can be found on busboy's page
fieldNameSize
- integer - Max field name size (Default: 100 bytes)fieldSize
- integer - Max field value size (Default: 1MB)fields
- integer - Max number of non-file fields (Default: Infinity)fileSize
- integer - For multipart forms, the max file size (in bytes) (Default: Infinity)files
- integer - For multipart forms, the max number of file fields (Default: Infinity)parts
- integer - For multipart forms, the max number of parts (fields + files) (Default: Infinity)headerPairs
- integer - For multipart forms, the max number of header key=>value pairs to parse Default: 2000 (same as node's http).limits: {
fieldNameSize: 100,
files: 2,
fields: 5
}
Specifying the limits can help protect your site against denial of service (DoS) attacks.
Function to rename the uploaded files. Whatever the function returns will become the new name of the uploaded file (extension is not included). The fieldname
and filename
of the file will be available in this function, use them if you need to.
rename: function (fieldname, filename) {
return fieldname + filename + Date.now()
}
Event handler triggered when a file starts to be uploaded. A file object with the following properties are available to this function: fieldname
, originalname
, name
, encoding
, mimetype
, path
, extension
.
onFileUploadStart: function (file) {
console.log(file.fieldname + ' is starting ...')
}
You can even stop a file from being uploaded - just return false
from the event handler. The file won't be processed or reach the file system.
onFileUploadStart: function (file) {
if (file.originalname == 'virus.exe') return false;
}
Event handler triggered when a chunk of buffer is received. A buffer object along with a file object is available to the function.
onFileUploadData: function (file, data) {
console.log(data.length + ' of ' + file.fieldname + ' arrived')
}
Event handler trigger when a file is completely uploaded. A file object is available to the function.
onFileUploadComplete: function (file) {
console.log(file.fieldname + ' uploaded to ' + file.path)
}
Event handler triggered when the form parsing starts.
onParseStart: function () {
console.log('Form parsing started at: ', new Date())
}
Event handler triggered when the form parsing completes. The request
object and the next
objects are are passed to the function.
onParseEnd: function (req, next) {
console.log('Form parsing completed at: ', new Date());
// usage example: custom body parse
req.body = require('qs').parse(req.body);
// call the next middleware
next();
}
Note: If you have created a onParseEnd
event listener, you must manually call the next()
function, else the request will be left hanging.
Event handler for any errors encountering while processing the form. The error
object and the next
object is available to the function. If you are handling errors yourself, make sure to terminate the request or call the next()
function, else the request will be left hanging.
onError: function (error, next) {
console.log(error)
next(error)
}
Event handler triggered when the number of files exceed the specification in the limit
object. No more files will be parsed after the limit is reached.
onFilesLimit: function () {
console.log('Crossed file limit!')
}
Event handler triggered when the number of fields exceed the specification in the limit
object. No more fields will be parsed after the limit is reached.
onFieldsLimit: function () {
console.log('Crossed fields limit!')
}
Event handler triggered when the number of parts exceed the specification in the limit
object. No more files or fields will be parsed after the limit is reached.
onPartsLimit: function () {
console.log('Crossed parts limit!')
}
Copyright (c) 2014 Hage Yaapa <http://www.hacksparrow.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Middleware for handling `multipart/form-data`.
The npm package multer receives a total of 0 weekly downloads. As such, multer popularity was classified as not popular.
We found that multer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
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.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.