title: node.js formidable with express.js
body: <p>I am new to node.js and learning it from various sources such as bootcamps, websites, etc.
  I want to upload a file using formidable module in node.js and express.js framework. Everytime I run this code it show an error....</p>
  
  <pre><code>  var oldpath = file.fileupload.path;
                                     ^
    TypeError: Cannot read property 'path' of undefined
  </code></pre>
  
  <p>I have used body parser to receive the name of the file.</p>
  
  <p>Node.js code:
  <div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var express = require("express");
  var app = express();
  var bodyParser = require("body-parser");
  var formidable = require("formidable");
  var fs = require("fs");
  var PORT = process.env.PORT || 5000
  
  app.set("view engine","ejs");
  app.use(bodyParser.urlencoded({extended: true}));
  
  app.get("/" , function(req, res){
   res.render("form");
  });
  app.post("/fileupload" , function(req, res){
   var fileupload = req.body.filetoupload;
      var form =  new formidable.IncomingForm();
      form.parse(req, function(err, fields, files){
        var oldpath = files.fileupload.path;
           var newpath = "C:/Users/ayush/"+files.fileupload.name;
           fs.rename(oldpath, newpath, function(err){
            if(err)
             console.log(err);
            else{
                  res.write("File Uploaded");
                  res.end();
              }
           }); 
      });
  });
  
  app.listen(PORT, function(){
   console.log("Server started");
  });</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
   &lt;title&gt;FileUpload&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
   &lt;form action="/fileupload" method="POST"&gt;
          &lt;label&gt;
           File: 
         &lt;input type="file" name="filetoupload" enctype="multipart/form-data"&gt;
          &lt;/label&gt;
    &lt;button&gt;Submit&lt;/button&gt;
   &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>