0

I want to present my index.html when request.url = '/'. However it seems as if the extension files related to my index.html file aren't being rendered.

Here is my server.js file:

var verifyMimeType = true;
var port = 8000;
var serverURL = "127.0.0.1";

console.log("Starting web server: " + serverURL + ":" + port);

var server = http.createServer(function(req,res){
  var parsedURL = url.parse(req.url);
  console.log("Parsed URL: " + parsedURL);

  if(req.url == '/'){
    filename = '/public/index.html'
  }
  console.log("Filename is: " + filename);
  // sets the extention of the filename
  var ext = path.extname(filename);
  var localPath = __dirname;
  console.log("Local path: "+ localPath);
  var validExtentions ={
    ".html" : "text/html",
    ".js": "application/javascript",
    ".css": "text/css",
    ".txt": "text/plain",
    ".jpg": "image/jpeg",
    ".gif": "image/gif",
    ".png": "image/png"
  };

  var validMimeType = true;
  var mimeType = validExtentions[ext];
  if(verifyMimeType){
    validMimeType = validExtentions[ext] != undefined;
  }

  if(validMimeType){
    localPath += filename;
    fs.exists(localPath, function(exists){
      if(exists){
        console.log("Serving file: " + localPath);
        getFile(localPath,res,mimeType);
      }
      else{
        console.log("File not found: " + localPath);
        res.writeHead(404);
        res.end();
      }
    });
  }
  else{
    console.log("Invalid file extention detected: " + ext);
    console.log("Invalid file name: " + filename);
  }
});

server.listen(port,serverURL);

var getFile = function(localPath, res, mimeType){
  fs.readFile(localPath, function(err, data){
    if(err){
      console.log("Error with reading file: ("+ err + ")");
      res.writeHead(500);
      res.end();
    }
    else{
      res.setHeader("Content-Length", data.length);
      if(mimeType != undefined){
        res.setHeader("Content-Type", mimeType);
      }
      res.statusCode = 200;
      // the end does two things, it write to the response and
      // ends the response.
      res.end(data);
    }
  });
}
4
  • You have quite a bit of logging. What does the log say for one such failing request? Commented Jan 18, 2017 at 8:50
  • it just says that im parsing through the same file name(or public/index.html) over and over again Commented Jan 18, 2017 at 9:00
  • Could you just paste the whole log of a single request? Commented Jan 18, 2017 at 9:01
  • Filename is: /public/index.html Local path: /Users/.../Desktop/myProjects/EnRoute Serving file: /Users/..../Desktop/myProjects/EnRoute/public/index.html Commented Jan 18, 2017 at 9:37

1 Answer 1

1

You forget to initialize filename when the url is not /. Here's a sample fix:

if(req.url == '/') {
  filename = '/public/index.html'
} else {
  filename = path.basename(parsedURL.pathname);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.