Gildas.Tambo
Gildas.Tambo

Reputation: 22663

how to load js and css file with nodejs

I am having a hard time loading css and js files with nodejs, here is what i have so far

The Js

/** Install nodeJs server the easy way
 *
 *  $ npm install http-server -g     // install http server *  globally
 *  $ cd MyApp                       // navigate to the app's root folder
 *  $ http-server                    //run the server
 *
**/

console.log("Welcome to sandy server");
//Module requires
var http = require("http"),   //load http node module
	fs = require("fs"),       //load file system
	path = require('path'),
	url = require('url'),
    data = require('./data'); //load file index.js in sub-folder data
    
    
//Object "MIMETYPES"
var MIMETYPES = {
		html: "text/html; charset=utf-8",
		jpeg: "image/jpeg",
		jpg: "image/jpeg",
		png: "image/png",
		js: "text/javascript",
		css: "text/css"
	},
	PORT = 8080;


http.createServer(function (request, response){ //Returns a new instance of http.Server.
    console.log("request well recieved",request.url);
	var listener = function (error, contentType){
	    if(error){
          console.log('DIE!', error);
          if(contentType !== "undefined"){
	          response.writeHeader(500, {"Content-Type": contentType});
          }else{
	          response.writeHeader(500, {"Content-Type": "text/plain"}); 
          }
          response.end("<h1>FS READ FILE ERROR: Internal Server Error!</h1>");    
        }
    };
    
    var fileArray = request.url.split('.', 2),
        filenameExtension = fileArray[1];
    
    
    if( MIMETYPES.hasOwnProperty(filenameExtension) ){
    	console.log("MIMETYPES::: ",MIMETYPES[filenameExtension]);
        fs.readFile(request.url, function (error, fileContent){
        	listener(error,MIMETYPES[filenameExtension]);
        	response.writeHead(200, {'Content-Type': MIMETYPES[filenameExtension]});
        	response.write(fileContent);
			response.end(); 
		});
    }
    else{
    	console.log("::: Not in MIMETYPES ::: ");
    	fs.readFile("./spa.html", function (error, fileContent){
    		listener(error);
			response.writeHead(200, {'Content-Type': MIMETYPES["html"]});
			response.write(fileContent);
			response.end();      
    	});
		
    }
	console.log("end of the request");
}).listen(PORT, '127.0.0.1');//run the server on port
console.log("Server running at http://127.0.0.1:" + PORT + "/  see you next time!");

terminal

enter image description here

Chrome console

enter image description here

enter image description here

enter image description here

can anybody help me on that or tell me what i am doing wrong ? i just want to use nodejs not express or something thank you.

Upvotes: 2

Views: 803

Answers (1)

zaynetro
zaynetro

Reputation: 2308

The problem is that you are trying to open files using root location. request.url returns /css/init.css and you are reading a file from the same location. Prepend it with . or resolve the relative path from your script location.

Upvotes: 1

Related Questions