3

I have multer in my app.js like :

var multer  = require('multer');
app.use(multer({ dest: './public/img/profile/',
 rename: function (fieldname, filename) {
    return fieldname;
  },
onFileUploadStart: function (file) {
  console.log(file.originalname + ' is starting ...')
},
limits: {
  files: 1
},
onFileUploadComplete: function (file) {
  console.log(file.fieldname + ' uploaded to  ' + file.path)
  imageUploaded=true;
}
}));

This works but I want to set these settings in a secific route. for example imageRoute.js and not in my app.js. But what should I use instead of app.use() than because I cant access app in my routes. I create routes in my app.js like:

  var imageRoutes  = require('./routes/imageRoutes')();
  app.use('/image', imageRoutes);

imageRoutes.js

module.exports = function(passport) {

var that = this;

// Modules
var express = require('express');
var router = express.Router();
var fs = require('fs');

// Middleware: Checks if user is authenticated removed for more readable code


router.post('/fileupload', that.ensureAuthenticated, function(req, res, next) {
   //setup multer here 
});

 router.post('/fileupload2', that.ensureAuthenticated, function(req, res, next) {
   //setup multer here with a different destination path/filename etc
});




return router;
}

i've found this question but it didnt really help me.

UPDATE

I now edited my app.js with :

 var multer  = require('multer');
 var imageRoutes= require('./routes/imageRoutes')(someRepo, multer);

imageRoutes.js

// POST: CREATE avatar
router.post('/avatar', function(req, res) {
    router.use(multer({
        dest: './public/img/profile/',
        rename: function (fieldname, filename) {
            return fieldname;
        },
        onFileUploadStart: function (file) {
            console.log(file.originalname + ' is starting ...')
        },
        limits: {
            files: 1
        },
        onFileUploadComplete: function (file) {
            console.log(file.fieldname + ' uploaded to  ' + file.path)
            imageUploaded=true;
            console.log(req.files);
            res.redirect('/');
        }
    }))

}); 

I see chrome is uploading a file to 100% but then it does nothing. it dosn't log is starting.. or any other error.

Community
  • 1
  • 1
Sven van den Boogaart
  • 11,833
  • 21
  • 86
  • 169

1 Answers1

4

You can pass multiple middleware/routers to .use():

var multer  = require('multer');
var parseUploads = multer({
  dest: './public/img/profile/',
  rename: function (fieldname, filename) {
    return fieldname;
  },
  onFileUploadStart: function (file) {
    console.log(file.originalname + ' is starting ...')
  },
  limits: {
    files: 1
  },
  onFileUploadComplete: function (file) {
    console.log(file.fieldname + ' uploaded to  ' + file.path)
    imageUploaded=true;
  }
});

// ...

var imageRoutes  = require('./routes/imageRoutes')();
app.use('/image', parseUploads, imageRoutes);

If you want to move the logic completely to imageRoutes.js and your multer logic is route-specific, you could do something like:

router.post('/avatar', multer({
  dest: './public/img/profile/',
  rename: function (fieldname, filename) {
    return fieldname;
  },
  onFileUploadStart: function (file) {
    console.log(file.originalname + ' is starting ...')
  },
  limits: {
    files: 1
  },
  onFileUploadComplete: function (file) {
    console.log(file.fieldname + ' uploaded to  ' + file.path)
  }
}), function(req, res) {
  // Here you can check `Object.keys(req.files).length`
  // or for specific fields like `req.files.imageField`
  res.redirect('/');
});
mscdex
  • 104,356
  • 15
  • 192
  • 153
  • But if i have for instance 5 different routes that upload images with different destinatinos etc my app.js will be a mess. I trie to set all the data in the route file not in the app.js. Thanks for helping though – Sven van den Boogaart May 20 '15 at 18:04
  • Ok, so then just move your `multer` usage to your `imageRoutes.js` file and have `router.use(parseUploads);` or `router.use(multer({...}))`. – mscdex May 20 '15 at 18:14
  • 1
    i've updated my answer to include an example of what i had in mind. if your `multer` usage isn't *route-specific*, you could just put `router.use(multer({..}))` somewhere near the top of your `imageRoutes.js` (not inside an actual route handler) or you could create a function that returns `multer({ ... })` except with a variable `dest` path if all that differs between routes is the path. – mscdex May 20 '15 at 19:10
  • Thanks for the help (console.log(req.files) need to be removed req not available) . Last question, how should i check the imageUploaded var its not available at the moment – Sven van den Boogaart May 20 '15 at 19:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78352/discussion-between-sven-b-and-mscdex). – Sven van den Boogaart May 20 '15 at 20:06
  • The new versions require you to call either `.array()`, `.single()`, `.fields()`, etc, on the object returned by `multer` to get this worked. Refer [https://github.com/expressjs/multer](https://github.com/expressjs/multer) for more info. – Ahmad Baktash Hayeri Mar 23 '17 at 06:35