title: Cannot call method 'toString' of null in javascript - nodejs
body: <p>I am new to JS and trying to write a simple webserver. Here is my prj browser.
  When I go to browser -<a href="http://localhost:4000/home.html" rel="nofollow noreferrer">http://localhost:4000/home.html</a></p>
  
  <p>I get the error - Cannot call method 'toString' of null.</p>
  
  <p>The problem is that UrlResLoader.httpcode and UrlResLoader.fileType isn't defined</p>
  
  <pre><code>  var UrlResLoader = new UrlLoader();
             UrlResLoader.requestUrl(fileResEngine);
             res.writeHead(UrlResLoader.httpcode, UrlResLoader.fileType);
             res.write(UrlResLoader.data);
             res.end()
  </code></pre>
  
  <p>I am not sure what is the problem here, I have hooked a debugger and have found that the problem happens on the   fs.readFile(fileResEngine.fullpath, function (err, data).</p>
  
  <p>I am still unclear on why is it happening. After researching a bit, I found that inorder to invoke closure functions, I should save the "this" pointer to refer the member varaibles. Other wise, the instance will be different. 
  But, this hasn't fixed the problem.
  Also, any design flaws or comments will be welcomed here.</p>
  
  <p><a href="https://i.stack.imgur.com/wg2ie.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wg2ie.png" alt="enter image description here"></a></p>
  
  <p>Here is my code -</p>
  
  <p>The main file -</p>
  
  <pre><code>const http = require('http');
  const parseUrl = require('parseurl');
  
  
  const fileEngine = require('./fileErrHandler');
  const UrlLoader = require('./urlController');
  
  http.createServer( function (req, res)
  {
      try
      {
  
          // this is a library function
          var pathName = decodeURIComponent(parseUrl(req).pathname);
  
          var fileResEngine=  new fileEngine(pathName);
  
          // create a literal validateFile to validate the path
          fileResEngine.pathCheck();
          if (fileResEngine.error === true )
          {
              res.statusCode = fileResEngine.statusCode;
              res.end(fileResEngine.ErrorMsg);
  
              return;
          }
  
          else
          {
             var UrlResLoader = new UrlLoader();
             UrlResLoader.requestUrl(fileResEngine);
             res.writeHead(UrlResLoader.httpcode, UrlResLoader.fileType);
             res.write(UrlResLoader.data);
             res.end();
          }
  
      }
      catch(err)
      {
          res.statusCode = err.status || 500;
          res.end(err.message);
      }
  
  }).listen(4000);
  </code></pre>
  
  <p>The file error handler</p>
  
  <pre><code>var resolvePath = require('resolve-path');
  const path = require('path');
  var pagesDir = path.join(__dirname, 'Pages');
  
  
  
      function pathCheckerEngine(path)
      {
          this.error = true;
          this.path = path;
          this.statusCode = 500;
          this.ErrorMsg = "Internal Server Error";
          this.PageRequest = "home.html";
          this.extname = "html";
          this.fullpath = './';
          var pcEngine = this;
          this.pathCheck = function()
          {
  
             try {
                 if (!path) {
                     pcEngine.statusCode = 400;
                     pcEngine.ErrorMsg = 'path required';
                     pcEngine.error = true;
                 }
  
                 else {
  
                     //removes first '/' of the path
                     pcEngine.PageRequest = path.substr(1);
                     pcEngine.fullpath = resolvePath(pagesDir, this.PageRequest);
  
                     pcEngine.statusCode = 200;
                     pcEngine.ErrorMsg = null;
                     pcEngine.error = false;
                     pcEngine.extname =  this.PageRequest.split('.').pop();
  
                 }
             }
             catch(err)
             {
                 pcEngine.statusCode = err.status || 500;
                 pcEngine.ErrorMsg = 'Malicious Page Request';
                 pcEngine.error = true;
             }
          }
      }
      module.exports = pathCheckerEngine;
  </code></pre>
  
  <p>And the final file </p>
  
  <pre><code>const fileEngine = require('./fileErrHandler');
  
  const fs = require('fs');
  const mime = require('mime');
  
      function UrlController(fileResEngine) {
          this.httpcode = null;
          this.fileType = null;
          this.data = null;
          var urctrl = this;
          this.requestUrl = function (fileResEngine) {
  
  
              switch (fileResEngine.extname) {
  
                  case 'html':
                      fs.readFile(fileResEngine.fullpath, function (err, data) {
                          if (err)
                          {
                              console.log(err);
                              urctrl.httpcode = 404;
                              urctrl.data = "Page not found";
                              return;
                          }
                          urctrl.httpcode = 200;
                          urctrl.fileType = "'Content-Type': 'text/html'";
                          urctrl.data = data;
  
                      });
  
                      break;
  
                  case 'png':
                  case 'jpeg':
                  case 'jpg':
                  case 'bmp':
                      fs.readFile(fileResEngine.fullpath, function (err, data) {
                          if (err) {
                              console.log(err);
                              urctrl.httpcode = 404;
                              urctrl.data = "File not found";
                              return;
                          }
                          urctrl.httpcode = 200;
                          urctrl.fileType = mime.lookup(mime.lookup('./images' + req.url));
                          urctrl.data = data;
  
                      });
  
                      break;
  
                  default:
                      urctrl.httpcode = 404;
                      urctrl.data = "Page not Found"
                      break;
  
              }
          }
  
          return;
      }
  
      module.exports = UrlController;
  </code></pre>