Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Update multer middleware in config/lib/express.js #947

Closed
gustavodemari opened this issue Sep 30, 2015 · 2 comments
Closed

Update multer middleware in config/lib/express.js #947

gustavodemari opened this issue Sep 30, 2015 · 2 comments
Assignees
Milestone

Comments

@gustavodemari
Copy link
Contributor

Hi guys,

The multer library has changed, and also they've changed the way of using multer as a middleware in express.

Old way:

var express = require('express')
var multer  = require('multer')

var app = express()
app.use(multer({ dest: './uploads/'}))

New way:

var express = require('express')
var multer  = require('multer')
var upload = multer({ dest: 'uploads/' })

var app = express()

app.post('/profile', upload.single('avatar'), function (req, res, next) {
  // req.file is the `avatar` file
  // req.body will hold the text fields, if there were any
})

app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
  // req.files is array of `photos` files
  // req.body will contain the text fields, if there were any
})

var 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 (String -> Array) where fieldname is the key, and the value is 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
})

As you can see below, we are using the old way of usage in the config/lib/express.js#L92-L97

  // Add multipart handling middleware
  app.use(multer({
    dest: './uploads/',
    inMemory: true
  }));
};

At the same time, we are using the parameter inMemory:true, that can lead our apps to run out of memory as pointed out here https://github.com/expressjs/multer#memorystorage

Also, I tried to remove the parameter inMemory (it's the same to set inMemory:false) and had an issue changing the profile image, because our controller uses the buffer (req.files.file.buffer) to change the profile picture.

So, my suggestion is to update the way we use multer, and maybe, remove the parameter inMemory:true and adjust the controller to don't use req.files.file.buffer

@codydaig
Copy link
Member

@gustavodemari Submit a PR!

@ezerw
Copy link

ezerw commented Oct 22, 2015

Creating "uploads" folder in "/modules/users/client/img/profile" works for me (multer old way).

@ilanbiala ilanbiala self-assigned this Oct 26, 2015
@ilanbiala ilanbiala added this to the 0.4.2 milestone Oct 26, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants