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");