title: About using Multer(1.0.6) and Express to upload files
body: <p>I want to use multer to upload images withe express from a form.</p>
  
  <p>I find a <a href="https://codeforgeek.com/2014/11/file-uploads-using-node-js/" rel="nofollow">post</a> about how to use multer to upload file, and trigger events to rename the file. However, I found it only works in old version (multer-0.1.6)</p>
  
  <p>In the latest instruction in multer's <a href="https://github.com/expressjs/multer" rel="nofollow">github</a>, I think I have to use <code>disk storage engine</code> to upload the files with new name, however it does not works. Please give me some instructions, I tried this function whole day...</p>
  
  <p>Moreover, I am not sure why <code>res.status(204).end();</code> is needed? I I don't add this line, the webpage will always lock when I click upload button...</p>
  
  <p><strong>package.json</strong></p>
  
  <pre><code>{
    "name": "FileUpload",
    "version": "0.0.0",
    "private": true,
    "scripts": {
      "start": "node ./bin/www"
    },
    "dependencies": {
      "body-parser": "~1.13.2",
      "cookie-parser": "~1.3.5",
      "debug": "~2.2.0",
      "express": "~4.13.1",
      "jade": "~1.11.0",
      "morgan": "~1.6.1",
      "multer": "^1.0.6",
      "serve-favicon": "~2.3.0"
    }
  }
  </code></pre>
  
  <p><strong>upload.js</strong></p>
  
  <pre><code>var express = require('express');
  var multer  = require('multer');
  var upload = multer({ storage: storage });
  
  var app = express();
  
  app.post('/uploadImage', upload.single('image'), function (req, res, next) {
      console.log("success");
      console.log(req.file);
      res.status(204).end();
  });
  
  app.get('/',function(req,res){
      console.log("It's work");
      res.sendFile(__dirname + "/index.html");
  });
  
  var storage = multer.diskStorage({
     destination: function (req, file, cb) {
       console.log("Dest");
       cb(null, 'uploads/')
     },
     filename: function (req, file, cb) {
       cb(null, file.fieldname + '-' + Date.now())
     }
  });
  
  app.listen(3000,function(){
      console.log("Working on port 3000");
  });
  </code></pre>
  
  <p><strong>index.html</strong></p>
  
  <pre><code>&lt;form id        =  "uploadForm"
       enctype   =  "multipart/form-data"
       action    =  "/uploadImage"
       method    =  "post"
  &gt;
  &lt;input type="file" name="image" /&gt;
  &lt;input type="submit" value="Upload Image" name="submit"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>Solution</strong>
  The reason for why my code can not run is the code's order...
  The following order is right.</p>
  
  <pre><code>var express = require('express');
  var multer  = require('multer');
  
  var app = express();
  
  app.get('/',function(req,res){
      console.log("It's work");
      res.sendFile(__dirname + "/index.html");
  });
  
  var storage = multer.diskStorage({
     destination: function (req, file, cb) {
       console.log("Dest");
       cb(null, 'uploads/')
     },
     filename: function (req, file, cb) {
       cb(null, file.fieldname + '-' + Date.now())
     }
  });
  
  var upload = multer({ storage: storage });
  
  app.post('/uploadImage', upload.single('image'), function (req, res, next) {
      console.log("success");
      console.log(req.file);
      res.status(204).end();
  });
  
  app.listen(3000,function(){
      console.log("Working on port 3000");
  });
  </code></pre>