I would like to know how I could make my app.js so that I could access any .js or .css file from my index.html or any other html page placed in my "public" folder. I have the .css and .js files placed in different "css" or "javascript" folders inside "public" folder but I dont really know how to make my index.html connect to them without the css and javascript placed directly in the public folder with no subfolders for them to be in.
Is it possible to make it so that all my files in "public" folder can connect without me having to add them into the app.js all the time?
This is the app.js:
var
http = require('http'),
path = require('path'),
fs = require('fs'),
extensions = {
".html" : "text/html",
".css" : "text/css",
".js" : "application/javascript",
".png" : "image/png",
".gif" : "image/gif",
".jpg" : "image/jpeg"
};
function getFile(filePath,res,page404,mimeType){
fs.exists(filePath,function(exists){
if(exists){
fs.readFile(filePath,function(err,contents){
if(!err){
res.writeHead(200,{
"Content-type" : mimeType,
"Content-Length" : contents.length
});
res.end(contents);
} else {
console.dir(err);
};
});
} else {
fs.readFile(page404,function(err,contents){
if(!err){
res.writeHead(404, {'Content-Type': 'text/html'});
res.end(contents);
} else {
console.dir(err);
};
});
};
});
};
function requestHandler(req, res) {
var
fileName = path.basename(req.url) || '/index/index.html',
ext = path.extname(fileName),
localFolder = __dirname + '/public/',
page404 = localFolder + '404.html';
if(!extensions[ext]){
res.writeHead(404, {'Content-Type': 'text/html'});
res.end("<html><head></head><body>The requested file type is not supported</body></html>");
};
getFile((localFolder + fileName),res,page404,extensions[ext]);
};
http.createServer(requestHandler)
.listen(process.env.PORT, process.env.IP);
Any help would be greatly appreciated and just tell me if you need me to list up my folder structure (though I wouldn't know why that would be neccessary...) :)