title: How to send a file to Nodejs using FormData and have Node send back a confirmation message?
body: <p>Hi I'm working on this simple form trying to send a file to my Nodejs server using <code>FormData</code>, but for some reason Node never receives it. Also How can I have Node send back a confirmation message on the page saying that file was received. What is it that I'm doing wrong or missing?. Please help. Thank you in advance. Here's my code.</p>
  
  <p><strong>HTML</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;&lt;/title&gt;
  &lt;script type="text/javascript"&gt;
   var fd = new FormData();   
   fd.append( 'file', input.files[0] );
  
  $.ajax({
    url: '/uploadfile',
    data: fd,
    processData: false,
    contentType: false,
    type: 'POST',
    success: function(data){
    alert(data);
    }
  });
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
   &lt;form enctype='multipart/form-data'&gt;
     &lt;input type= "text" name = "theName"/&gt;
     &lt;input type="file" id="file" name="file"&gt;
     &lt;input type="submit"&gt;
  &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>Server (Nodejs)</strong></p>
  
  <pre><code> var express = require('express');
   var router = express.Router();
  
   /* GET users listing. */
  
   router.get('/', function(req, res){
   res.sendfile('./public/html/form-file.html');
   });
  
   /* POST file */
   router.post('/', function(req , res){
  
   console.log('Request received: ');
   console.log(req.body) // this line helps inspect the request so you can see    whether the data is in the url (GET) or the req body (POST)
   console.log('\nRequest recieved: \nmethod: ' + req.method + '\nurl: ' +  req.url) // this line logs just the method and url
  
   res.end('callback(\'{\"msg\": \"OK\"}\')');
  
   });
  
   module.exports = router;
  </code></pre>