title: How to set different destinations in nodejs using multer?
body: <p>I'm trying to upload any file using <strong><a href="https://www.npmjs.org/package/multer" rel="noreferrer">Multer package</a></strong>. It's working fine when I use following code in my <code>server.js</code> file.</p>
  
  <pre><code>var express = require('express'),
      app = express(),
      multer = require('multer');
  app.configure(function () {
      app.use(multer({
          dest: './static/uploads/',
          rename: function (fieldname, filename) {
              return filename.replace(/\W+/g, '-').toLowerCase();
          }
      }));
      app.use(express.static(__dirname + '/static'));
  });
  
  app.post('/api/upload', function (req, res) {
      res.send({image: true, file: req.files.userFile.originalname, savedAs: req.files.userFile.name});
  });
  
  var server = app.listen(3000, function () {
      console.log('listening on port %d', server.address().port);
  });
  </code></pre>
  
  <p>What I want is to store file at different locations. I had tried following code but it does not work for me.</p>
  
  <pre><code>var express = require('express'),
      app = express(),
      multer = require('multer');
  app.configure(function () {
      app.use(multer({
          //dest: './static/uploads/',
          rename: function (fieldname, filename) {
              return filename.replace(/\W+/g, '-').toLowerCase();
          }
      }));
      app.use(express.static(__dirname + '/static'));
  });
  
  app.post('/api/pdf', function (req, res) {
      app.use(multer({ dest: './static/pdf/'}));
      res.send({image: true, file: req.files.userFile.originalname, savedAs: req.files.userFile.name});
  });
  
  app.post('/api/image', function (req, res) {
      app.use(multer({ dest: './static/image/'}));
      res.send({image: true, file: req.files.userFile.originalname, savedAs: req.files.userFile.name});
  });
  
  app.post('/api/video', function (req, res) {
      app.use(multer({ dest: './static/video/'}));
      res.send({image: true, file: req.files.userFile.originalname, savedAs: req.files.userFile.name});
  });
  
  var server = app.listen(3000, function () {
      console.log('listening on port %d', server.address().port);
  });
  </code></pre>
  
  <p>Means, if I hit <code>http://localhost:3000/api/pdf</code> file should store at 'pdf' folder, if I hit <code>http://localhost:3000/api/video</code> file should store at 'video' folder.</p>
  
  <p>Is there any way to achieve this aim?</p>
  
  <p>Thank you in advance.</p>