Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Im having problem with my app.js in node not displaying my html page but my html code instead. I don't really know what to do so some help would be greatly appreciated. I would like to be able to load html, javascript and css from my public folder. This is my app.js code:

var http = require("http"),
    url = require("url"),
    path = require("path"),
    fs = require("fs")

http.createServer(function(request, response) {

var uri = url.parse(request.url).pathname
    , filename = path.join(process.cwd(), uri);

path.exists(filename, function(exists) {
  if(!exists) {
    response.writeHead(404, {"Content-Type": "text/plain"});
    response.write("404 Not Found\n");
    response.end();
    return;
}

if (fs.statSync(filename).isDirectory()) filename += 'public/index.html';

fs.readFile(filename, "binary", function(err, file) {
  if(err) {        
    response.writeHead(500, {"Content-Type": "text/plain"});
    response.write(err + "\n");
    response.end();
    return;
  }

  response.writeHead(200);
  response.write(file, "binary");
  response.end();
  });
 });
}).listen(process.env.PORT, process.env.IP);

console.log("Static file server running./\CTRL + C to shutdown");
share|improve this question
1  
Try response.writeHead(200, {'Content-Type': 'text/html'}); –  spacebean Jan 16 at 11:48
1  
Depending on what file it is you're trying to load you need to adjust the content type: text/html or text/css, for example. –  Andy Jan 16 at 11:48
    
thank you! I think its loading css and javascript too even tho I only added respnse.writeHead(200, {"Content-Type": "text/html"}); because I was able to link it to a css file? –  Rockyy Jan 16 at 12:02
    
oh wait no. How do I display all .css files? I was only able to use css if they were internal in the html page. Do i just add response.writehead(200, {"Content-Type": "text/css"}); in the app.js some random place? also the same for javascript? –  Rockyy Jan 16 at 13:51

1 Answer 1

You are not specifying the content type, if you try:

fs.readFile(filename, "binary", function(err, file) {
if(err) {        
    response.writeHead(500, {"Content-Type": "text/plain"});
    response.write(err + "\n");
    response.end();
    return;
  }

  response.writeHead(200, {'Content-Type': 'text/html'});
  response.write(file, "binary");
  response.end();
  });
 });
}).listen(process.env.PORT, process.env.IP);

That should work. If you want to display the error page in html, you will want to update that content type as well.

share|improve this answer
    
thanks! To display the error page in html would be awesome. I know how to write in html in the response.write(); but how do I direct it to a lets say 500.html error page? –  Rockyy Jan 16 at 11:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.