1// Installation
2// npm install --save multer
3
4// Usage
5var express = require('express')
6var multer = require('multer')
7var upload = multer({ dest: 'uploads/' })
8
9var app = express()
10
11app.post('/profile', upload.single('avatar'), function (req, res, next) {
12 // req.file is the `avatar` file
13 // req.body will hold the text fields, if there were any
14})
15
16app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
17 // req.files is array of `photos` files
18 // req.body will contain the text fields, if there were any
19})
20
21var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
22app.post('/cool-profile', cpUpload, function (req, res, next) {
23 // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files
24 //
25 // e.g.
26 // req.files['avatar'][0] -> File
27 // req.files['gallery'] -> Array
28 //
29 // req.body will contain the text fields, if there were any
30})