title: TypeError: Cannot read property 'length' of undefined in Node.js
body: <p>I'm trying to create a server but it is throwing me an error in console</p>
  
  <pre><code>$ node server1185
  Server listening at 1185
  
  C:\Users\Bharat\Desktop\Nodejs\server1185.js:9
  
  res.writeHead(200,{'Content-Type':'text/html', 'Content-Length': data.length
                                                                    ^
  
  TypeError: Cannot read property 'length' of undefined
      at ReadFileContext.callback (C:\Users\Bharat\Desktop\Nodejs\server1185.js:9:
  71)
      at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:420:13)
  </code></pre>
  
  <p>My Node.js code is following
  <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 http = require('http');
  var fs = require('fs');
  var server = http.createServer(function(req, res){
  displayForm(res);
  });
  
  function displayForm(res){
  fs.readFile('form.html', function(err,data){
  res.writeHead(200,{'Content-Type':'text/html', 'Content-Length': data.length
  });
  res.write(data);
  res.end();
  });
  }
  server.listen(1185);
  console.log('Server listening at 1185');</code></pre>
  </div>
  </div>
  
  It is throwing an error in data.length, an explanation will be appreciated. </p>