jquery - Index incorrectly loading both style.css and javascript -
so have webserver has handler function looks this
function handler(req,res){ var urlobj = url.parse(req.url,false); var root = "./files"; var filename = root + urlobj.pathname; //get method if(req.method ==='get'){ //grabbing index file if(filename === "./files/"){ console.log("get: " + filename + "index.html"); fs.readfile("./files/index.html", function(err,data){ if(err){ res.writehead(500); return res.end("error loading chatroom.html"); }else{ res.writehead(200, {"content-type": "text/html"}); res.end(data); } }); }else{ //this style.css *edit*-error within if if(filename === "./files/style.css"){ fs.readfile("./files/style.css", function(err,data){ if(err){ res.writehead(500); return res.end("error reading cssfile"); } res.writehead(200,{"content-type": "text/css"}); res.end(data); }) } //this script have written *edit*-error within if if(filename === "./files/theworks.js"){ fs.readfile("./files/theworks.js", function(err,data){ if(err){ res.writehead(500); return res.end("error reading javascript file"); } res.writehead(200,{"content-type": "text/javascript"}); res.end(data); }) } //end of else } } };
and client html looks this:
<!doctype html> <html> <head> <meta charset = "utf8"/> <script src="jquery-1.10.2.min.js"> </script> <title>webchat title</title> <link rel="stylesheet" type="text/css" href="style.css"> <script type="text/javascript" src="theworks.js"> </script> </head> <body> <div id="home"> <div id="table"> <h3> webchat <h3> <input id="chatapp" type="button" value="chat room" onclick="chatapp()"/> </div> </div> </body> </html>
and lastly javascript file looks this:
function chatapp(){ $(document).ready(function(){ }
when i'm loading page locally node.js , open developer tools, style.css file same javascript file. reverse. either way, i've done similar projects , has never occurred. thinking has me trying read files separately in server incorrectly. (using fs.readfile)
normally thought when read index.html initial "./files/" html automatically search script , css on it's own (all in same directory other the server 1 directory above html/style/javascript files) doesn't seem case.
this part of homework problem. however, asking not, , rest of javascript homework about. (which click on button load little socket based chat room). i've excluded sockets used in server file.
edit- if catch
Comments
Post a Comment