title: Renaming an uploaded file using Multer doesn't work (Express.js)
body: <p>I'm trying to upload a file from a HTML form using Express.js and Multer. I've managed to save the file to the desired location (a folder named <em>uploads</em>).</p>
  
  <p>However, I'd like to rename the file while uploading it because, by default, Multer gives it a strange name such as:</p>
  
  <blockquote>
    <p>5257ee6b035926ca99923297c224a1bb</p>
  </blockquote>
  
  <p>Might be a hexadecimal time stamp or so but I need a more explicit name in order to call a script on it later.</p>
  
  <p>I've followed the explanation found <a href="https://stackoverflow.com/questions/29214237/rename-a-file-with-multer-is-not-working">here</a> but it doesn't do anything more than it used to: uploading the file with the hexa name.</p>
  
  <p>Also, the two events <strong>onFileUploadStart</strong> and <strong>onFileUploadComplete</strong> never seem to be triggered as I don't get anything logged in my console.</p>
  
  <p>I am using two separate files for the server and the routing:</p>
  
  <p><strong><em>app.js</em></strong></p>
  
  <pre><code>/**
   * Dependencies
   */
  
  var express = require('express');
  var path = require('path');
  var logger = require('morgan');
  var cookieParser = require('cookie-parser');
  var bodyParser = require('body-parser');
  
  /**
   * Importation of routes
   */
  var routes = require('./routes/index');
  var recog = require('./routes/recog');
  
  /**
   * Express
   */
  var app = express();
  
  app.use(logger('dev'));
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({extended: false}));
  app.use(cookieParser());
  app.use(express.static(path.join(__dirname, 'public')));
  
  // pour contrer les erreurs de cross domain
  app.use(function (req, res, next) {
  
      // Website you wish to allow to connect
      res.setHeader('Access-Control-Allow-Origin', '*');
  
      // Request methods you wish to allow
      res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
  
      // Request headers you wish to allow
      res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
  
      // Set to true if you need the website to include cookies in the requests sent
      // to the API (e.g. in case you use sessions)
      res.setHeader('Access-Control-Allow-Credentials', true);
  
      // Pass to next layer of middleware
      next();
  });
  
  /**
   * Routes
   */
  app.use('/', routes);
  app.use('/recog', recog);
  
  module.exports = app;
  </code></pre>
  
  <p><strong><em>recog.js</em></strong></p>
  
  <pre><code>/**
   * Requirements
   */
  var express = require('express');
  var router = express.Router();
  var multer = require('multer');
  var uploads = multer({
      dest: 'uploads/',
      rename: function (fieldname, filename) {
          console.log("Rename...");
          return filename + Date.now();
      },
      onFileUploadStart: function () {
          console.log("Upload is starting...");
      },
      onFileUploadComplete: function () {
          console.log("File uploaded");
      }
  });
  
  /**
   * Upload d'une image
   */
  router.post('/upload', uploads.single('image'), function (req, res, next) {
      console.log("Front-end is calling");
      res.json({status: 'success', data: 'Fichier chargé.\nOrgane sélectionné : ' + req.body.organ});
  });
  
  module.exports = router;
  </code></pre>
  
  <p>I have been digging around but I can't figure out what the problem is as I am quite new to Node.js and JavaScript in general.</p>
  
  <p>Thanks for your help guys!</p>