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);
}
});
}
here is the new one.. in the end if done it with a else:
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/style.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(200, { 'Content-Type': contentType });
response.end(data);
}
else {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
}
});
}
Use the framework like the express for the web applications. So In express express.static will take care for the serving the static file to the client.
var express = require('express'); var app = express();
app.use(express.static('public'));
place the all the static files in the public directory and express will serve.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.