7
votes

First off: I'm not using Express.

With that out of the way, when I load my index.html file it recursively readFile every attached file such as my CSS and JS pages. But it always returns this error in my inspector (Chrome):

Resource interpreted as Script but transferred with MIME type text/plain

I have absolutely no idea why it is doing this. Here is my code:

var http = require('http');
var querystring = require('querystring');
var fs = require('fs');
var url = require('url');

function route(handle, pathname, response, request){
console.log("About to route a request for " + pathname);

if (typeof handle[pathname] === "function"){        
    handle[pathname](response, request);
    } else {

    var file_path = "";

    // parses the url request for a file and pulls the pathname
    var url_request = url.parse(request.url).pathname;      
    var tmp  = url_request.lastIndexOf(".");
    var extension  = url_request.substring((tmp + 1));

    file_path = url_request.replace("/", "");

    //load needed pages and static files
    fs.readFile(file_path, function (error, contents){
        if(error){
          console.log('DIE!');
          console.log(error);
          response.writeHeader(500, {"Content-Type": "text/html"});  
          response.end("<h1>FS READ FILE ERROR: Internal Server Error!</h1>");    
        }
        else{ 
          console.log('SUCCESS!');
          // set content type
          if (extension === 'html') response.writeHeader(200, {"Content-Type": 'text/html'});
          else if (extension === 'htm') response.writeHeader(200, {"Content-Type": 'text/html'});
          else if (extension === 'css') response.writeHeader(200, {"Content-Type": 'text/css'});
          else if (extension === 'js') response.writeHeader(200, {"Content-Type": 'text/javascript'});
          else if (extension === 'png') response.writeHeader(200, {"Content-Type": 'image/png'});
          else if (extension === 'jpg') response.writeHeader(200, {"Content-Type": 'image/jpg'});
          else if (extension === 'jpeg') response.writeHeader(200, {"Content-Type": 'image/jpeg'});
          else { console.log("NO CORRECT EXTENSION")};
            console.log(extension);
            response.end(contents);
        }

        response.end();  
    });
    response.end();
}
}

exports.route = route;

How can I fix this? This has stopped my project in its tracks.

Note: this is a progression of an earlier problem I had talked about here: Node.js incorrect path problems

2
First off, it’s writeHead, not writeHeader. Next — yikes, that if. You should use an object. response.writeHead(200, {'Content-Type': mimeTypes[extension]});.Ry-♦
Good catch on the writeHead. But as to the mime, see my previous question at stackoverflow.com/questions/18004512/… That was how I had it originally. It didnt make a difference. This is my second major iteration.JDillon522
Have you tried visiting the script URL directly? Does it work?Ry-♦
Yeah its pathing everything properly (if thats what you mean). And as best as I can tell from that stupid error, it reads it the right way, but then craps the bed and decides to change it.JDillon522
Why is that last response.end() statement there? Won't that be closing the response before fs.readFile completes?wdavo

2 Answers

8
votes

Try comment out that last response.end. Don't think you need it.

    response.end();  
    });
    // response.end();
   }
   exports.route = route;

I suspect that response.end is getting called immediately before the one inside the fs.readfile callback has time to execute, maybe closing off the headers on the server not giving the correct header "Content-Type": 'text/javascript' a chance to get sent..

Tried it on your code, worked for me..

1
votes

If nodejs and using express the below code works for me

      if (extension === 'html') res.set("Content-Type": 'text/html');
      else if (extension === 'htm') res.set("Content-Type": 'text/html');
      else if (extension === 'css') res.set("Content-Type": 'text/css');
      else if (extension === 'js') res.set("Content-Type": 'text/javascript');
      else if (extension === 'png') res.set("Content-Type": 'image/png');
      else if (extension === 'jpg') res.set("Content-Type": 'image/jpg');
      else if (extension === 'jpeg') res.set("Content-Type": 'image/jpeg');

The above code works form me.