{"action":"closed","issue":{"url":"https://api.github.com/repos/meanjs/mean/issues/947","labels_url":"https://api.github.com/repos/meanjs/mean/issues/947/labels{/name}","comments_url":"https://api.github.com/repos/meanjs/mean/issues/947/comments","events_url":"https://api.github.com/repos/meanjs/mean/issues/947/events","html_url":"https://github.com/meanjs/mean/issues/947","id":109002990,"number":947,"title":"Update multer middleware in config/lib/express.js","user":{"login":"gustavodemari","id":4651184,"avatar_url":"https://avatars.githubusercontent.com/u/4651184?v=3","gravatar_id":"","url":"https://api.github.com/users/gustavodemari","html_url":"https://github.com/gustavodemari","followers_url":"https://api.github.com/users/gustavodemari/followers","following_url":"https://api.github.com/users/gustavodemari/following{/other_user}","gists_url":"https://api.github.com/users/gustavodemari/gists{/gist_id}","starred_url":"https://api.github.com/users/gustavodemari/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gustavodemari/subscriptions","organizations_url":"https://api.github.com/users/gustavodemari/orgs","repos_url":"https://api.github.com/users/gustavodemari/repos","events_url":"https://api.github.com/users/gustavodemari/events{/privacy}","received_events_url":"https://api.github.com/users/gustavodemari/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/meanjs/mean/labels/enhancement","name":"enhancement","color":"84b6eb"}],"state":"closed","locked":false,"assignee":{"login":"ilanbiala","id":3695164,"avatar_url":"https://avatars.githubusercontent.com/u/3695164?v=3","gravatar_id":"","url":"https://api.github.com/users/ilanbiala","html_url":"https://github.com/ilanbiala","followers_url":"https://api.github.com/users/ilanbiala/followers","following_url":"https://api.github.com/users/ilanbiala/following{/other_user}","gists_url":"https://api.github.com/users/ilanbiala/gists{/gist_id}","starred_url":"https://api.github.com/users/ilanbiala/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ilanbiala/subscriptions","organizations_url":"https://api.github.com/users/ilanbiala/orgs","repos_url":"https://api.github.com/users/ilanbiala/repos","events_url":"https://api.github.com/users/ilanbiala/events{/privacy}","received_events_url":"https://api.github.com/users/ilanbiala/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/meanjs/mean/milestones/8","html_url":"https://github.com/meanjs/mean/milestones/0.4.2","labels_url":"https://api.github.com/repos/meanjs/mean/milestones/8/labels","id":1285066,"number":8,"title":"0.4.2","description":"","creator":{"login":"ilanbiala","id":3695164,"avatar_url":"https://avatars.githubusercontent.com/u/3695164?v=3","gravatar_id":"","url":"https://api.github.com/users/ilanbiala","html_url":"https://github.com/ilanbiala","followers_url":"https://api.github.com/users/ilanbiala/followers","following_url":"https://api.github.com/users/ilanbiala/following{/other_user}","gists_url":"https://api.github.com/users/ilanbiala/gists{/gist_id}","starred_url":"https://api.github.com/users/ilanbiala/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ilanbiala/subscriptions","organizations_url":"https://api.github.com/users/ilanbiala/orgs","repos_url":"https://api.github.com/users/ilanbiala/repos","events_url":"https://api.github.com/users/ilanbiala/events{/privacy}","received_events_url":"https://api.github.com/users/ilanbiala/received_events","type":"User","site_admin":false},"open_issues":7,"closed_issues":80,"state":"open","created_at":"2015-09-01T22:36:52Z","updated_at":"2015-10-26T02:03:26Z","due_on":null,"closed_at":null},"comments":2,"created_at":"2015-09-30T02:35:35Z","updated_at":"2015-10-26T02:03:26Z","closed_at":"2015-10-26T02:03:26Z","body":"Hi guys,\r\n\r\nThe multer library has changed, and also they've changed the way of using multer as a middleware in express.\r\n\r\n[Old way](https://github.com/expressjs/multer/blob/c36cf0c33656ef3e2ba76a6118de14e7b61e3c6d/README.md#usage):\r\n```js\r\nvar express = require('express')\r\nvar multer  = require('multer')\r\n\r\nvar app = express()\r\napp.use(multer({ dest: './uploads/'}))\r\n```\r\n\r\n[New way](https://github.com/expressjs/multer#usage):\r\n```javascript\r\nvar express = require('express')\r\nvar multer  = require('multer')\r\nvar upload = multer({ dest: 'uploads/' })\r\n\r\nvar app = express()\r\n\r\napp.post('/profile', upload.single('avatar'), function (req, res, next) {\r\n  // req.file is the `avatar` file\r\n  // req.body will hold the text fields, if there were any\r\n})\r\n\r\napp.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {\r\n  // req.files is array of `photos` files\r\n  // req.body will contain the text fields, if there were any\r\n})\r\n\r\nvar cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])\r\napp.post('/cool-profile', cpUpload, function (req, res, next) {\r\n  // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files\r\n  //\r\n  // e.g.\r\n  //  req.files['avatar'][0] -> File\r\n  //  req.files['gallery'] -> Array\r\n  //\r\n  // req.body will contain the text fields, if there were any\r\n})\r\n```\r\n\r\nAs you can see below, we are using the old way of usage in the [config/lib/express.js#L92-L97](https://github.com/meanjs/mean/blob/master/config/lib/express.js#L92-L97)\r\n```js\r\n  // Add multipart handling middleware\r\n  app.use(multer({\r\n    dest: './uploads/',\r\n    inMemory: true\r\n  }));\r\n};\r\n```\r\n\r\nAt 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\r\n\r\nAlso, 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](https://github.com/meanjs/mean/blob/master/modules/users/server/controllers/users/users.profile.server.controller.js#L51-L90) uses [the buffer (req.files.file.buffer)](https://github.com/meanjs/mean/blob/master/modules/users/server/controllers/users/users.profile.server.controller.js#L59) to change the profile picture.\r\n\r\nSo, 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"}}