Question, I'm working on a node.js app. simple no single application. I Have something done in the app.js file.. but the node.js not loading my .css files and .js files.
I have fixed it with the but the .css and .js files does
not loading i get this back. but i need the files in the output files not in the public/ folder.
would like to fix it. also my page looks and become this one but the source does not loading the css stylesheet or javascript files. i don't know why?
my structur :
-css
--style.css
-js
--javascript.js
-app.js
-public
--index.html
--add.html
here is my code :
var http = require('http');
var fs = require('fs');
http.createServer(function(request, response) {
console.log('request starting...');
var url = request.url;
switch(url) {
case '/' :
getStaticFileContent(response, 'public/index.html','text/html');
break;
case '/add':
getStaticFileContent(response, 'public/add.html','text/html');
break;
case '/css':
getStaticFileContent(response, 'css/style.css','text/css');
break;
case '/js':
getStaticFileContent(response, 'js/javascript.js','text/javascript');
break;
default:
response.writeHead(404, {'Content-Type':'text/plain'});
response.end('404 - Page not Found');
}
}).listen(8000);
console.log('Hello World, this is the Projekt Nr.1');
console.log('Server Running at localhost:80');
function getStaticFileContent(response, filepath, contentType) {
fs.readFile(filepath, function(error, data){
if(error) {
response.writeHead(500,{'Content-Type':'text/plain'});
response.end('500 - Internal Server Error');
}
if(data) {
response.writeHead(500,{'Content-Type':'text/html'});
response.end(data);
}
});
}