title: Upload files with node.js
body: <p>I have this code in order to upload files with node.js:</p>
  
  <pre><code>    app.use(express.bodyParser());
      // or, as `req.files` is only provided by the multipart middleware, you could 
      // add just that if you're not concerned with parsing non-multipart uploads, 
      // like:
      app.use(express.multipart());
  
      app.get('/',function(req,res){
      fs.readFile('uploadHTML.html',function (err, data){
          res.writeHead(200, {'Content-Type': 'text/html','Content-Length':data.length});
          res.write(data);
          res.end();
      });
  
      });
      app.post('/upload',function(req,res)
      {
      console.log(req.files);
      fs.readFile(req.files.displayImage.path, function (err, data) {
        // ...
        var newPath = __dirname;
        fs.writeFile(newPath, data, function (err) {
          res.redirect("back");
        });
      });
   });
  </code></pre>
  
  <p>Here is the HTML file:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Upload Example&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;form id="uploadForm"
        enctype="multipart/form-data"
        action="/upload"
        method="post"&gt;
    &lt;input type="file" id="userPhotoInput" name="displayImage" /&gt;
    &lt;input type="submit" value="Submit"&gt;
  &lt;/form&gt;
  
  &lt;span id="status" /&gt;
  &lt;img id="uploadedImage" /&gt;
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>When I upload the file, it gives me the next error:</p>
  
  <pre><code>TypeError: Cannot read property 'displayImage' of undefined at c:\NodeInstall\nodejs\express.js:42:22 at callbacks (c:\NodeInstall\nodejs\node_modules\express\lib\router\index.js:164:37) at param (c:\NodeInstall\nodejs\node_modules\express\lib\router\index.js:138:11) at pass (c:\NodeInstall\nodejs\node_modules\express\lib\router\index.js:145:5) at Router._dispatch (c:\NodeInstall\nodejs\node_modules\express\lib\router\index.js:173:5) at Object.router (c:\NodeInstall\nodejs\node_modules\express\lib\router\index.js:33:10) at next (c:\NodeInstall\nodejs\node_modules\express\node_modules\connect\lib\proto.js:193:15) at Object.expressInit [as handle] (c:\NodeInstall\nodejs\node_modules\express\lib\middleware.js:30:5) at next (c:\NodeInstall\nodejs\node_modules\express\node_modules\connect\lib\proto.js:193:15) at Object.query [as handle] (c:\NodeInstall\nodejs\node_modules\express\node_modules\connect\lib\middleware\query.js:45:5)
  </code></pre>
  
  <p>What could be the reason?</p>