From: paulr Date: Fri, 21 Dec 2012 08:43:01 +0000 (+1100) Subject: first commit X-Git-Url: http://git.pjr.cc/?a=commitdiff_plain;h=b6082f4e1e2d6fdb43966123e85ea9dfd1640bdc;p=nodejsws.git first commit --- b6082f4e1e2d6fdb43966123e85ea9dfd1640bdc diff --git a/app.js b/app.js new file mode 100644 index 0000000..e357dab --- /dev/null +++ b/app.js @@ -0,0 +1,10 @@ +var http = require("http"), + url = require("url"), + path = require("path"), + fs = require("fs"), + req = require("./lib/wsrequest.js"), + port = process.argv[2] || 8888; + +http.createServer(req.wsRequest).listen(port); + +console.log("Server running on port: ", port); \ No newline at end of file diff --git a/lib/myparse.js b/lib/myparse.js new file mode 100644 index 0000000..088dd7a --- /dev/null +++ b/lib/myparse.js @@ -0,0 +1,21 @@ +var path = require("fs"); + +function useBody() { + return; +} + +function parse(base, purl, response, servicer) { + var pathCheck = "./lib/web_"+base+".js"; + console.log("path check on ", pathCheck); + fs.exists(pathCheck, function (exists) { + if(!exists) console.log("not exists: ", base); + else { + servicer(purl, response, parse); + console.log("base exists: ", base); + } + }); +} + +exports.useBody = useBody; +exports.parse = parse; +exports.process = process; diff --git a/lib/purls/main.js b/lib/purls/main.js new file mode 100644 index 0000000..c5bf240 --- /dev/null +++ b/lib/purls/main.js @@ -0,0 +1,31 @@ +function serveMain(request, response, callback) { + response.writeHead(200); + + serveBody(request, response, function(request, response, callback) { + response.write("server main body"); + callback(request, response); + }); + return; +} + +function serveBody(request, response, bodyFunction) { + response.writeHead(200); + require("./main_head.js").go(request, response, function(request, response) { + response.write(""); + response.end(); + }); + }); + }); + }); + return; +} + + +exports.serveMain = serveMain; +exports.serveBody = serveBody; diff --git a/lib/purls/main_footer.js b/lib/purls/main_footer.js new file mode 100644 index 0000000..19f275e --- /dev/null +++ b/lib/purls/main_footer.js @@ -0,0 +1,7 @@ +function go(request, response, callback) { + response.write("

Copyright PJR.cc 2012"); + callback(request, response); + return; +} + +exports.go = go; \ No newline at end of file diff --git a/lib/purls/main_head.js b/lib/purls/main_head.js new file mode 100644 index 0000000..463d8dd --- /dev/null +++ b/lib/purls/main_head.js @@ -0,0 +1,8 @@ +function go(request, response, callback) { + console.log("in main_head.go"); + response.write("im a head"); + callback(request, response); + return; +} + +exports.go = go; \ No newline at end of file diff --git a/lib/purls/main_menu.js b/lib/purls/main_menu.js new file mode 100644 index 0000000..042b8a8 --- /dev/null +++ b/lib/purls/main_menu.js @@ -0,0 +1,8 @@ +function go(request, response, callback) { + console.log("in main_head.go"); + response.write("

Title

"); + require("./main_menu.js").go(request, response, function(request, response) { + response.write("
"); + bodyFunction(request, response, function(request, response) { + response.write("
"); + require("./main_footer.js").go(request, response, function(request, response) { + response.write("
menuitem1item2item3
"); + callback(request, response); + return; +} + +exports.go = go; \ No newline at end of file diff --git a/lib/wsrequest.js b/lib/wsrequest.js new file mode 100644 index 0000000..c7797fc --- /dev/null +++ b/lib/wsrequest.js @@ -0,0 +1,97 @@ +var url = require("url"); +var path = require("path"); +var fs = require("fs"); +var myparse = require("./myparse.js"); +var webmain = require("./purls/main.js"); + +var staticExtensions = ["html", "gif", "jpg", "css", "js", "ico"]; + +function wsRequest(request, response) { + var isStatic = 0; + var purl = url.parse(request.url); + + staticExtensions.forEach(function testExtn(setest) { + console.log("testing url: ", request.url); + console.log("against: ", purl.pathname); + var chk = purl.pathname.split("."); + console.log("chk: ", chk); + var chkid = staticExtensions.indexOf(chk[chk.length-1].toLowerCase()); + console.log("chkid is ", chkid); + if(chkid != -1) { + isStatic = 1; + } + }); + + if(isStatic == 1) { + // do the static + var servethis = purl.pathname.split("/").pop(); + serveStatic(servethis, response); + return; + } + + //console.log("request: ", request); + console.log("purl: ", purl); + + // now we need to find the extension used + // to serve the request based no the first purl + if(purl.pathname == "/") { + console.log("Serv main"); + webmain.serveMain(request, response, function (request, response) { + response.end(); + }); + return; + } + + var thispurl = purl.pathname.split("/")[1]; + fs.stat("./lib/purls/web_"+thispurl+".js", function (err, stats) { + console.log("get purl is "+thispurl+" and err "+err+" and "+stats); + if(err) { + response.writeHead(404, {"Content-Type": "text/plain"}); + response.write("404 Not Found\n"); + response.end(); + return; + } + var thiserv = require("./purls/web_"+thispurl+".js"); + if(thiserv.requireBody()) { + console.log("yubber is true"); + webmain.serveBody(request, response, thiserv.process); + } else { + console.log("yubber is false"); + thiserv.process(request, response, function(request, response) { + response.end(); + }); + } + return; + }); + + + return; +} + +function serveStatic(staticname, response) { + var pathName = "./res/"+staticname; + console.log("Pathname for check is ", pathName); + fs.exists(pathName, function(exists) { + if(!exists) { + response.writeHead(404, {"Content-Type": "text/plain"}); + response.write("404 Not Found\n"); + response.end(); + return; + } + + fs.readFile(pathName, "binary", function(err, file) { + if(err) { + response.writeHead(500, {"Content-Type": "text/plain"}); + response.write(err + "\n"); + response.end(); + return; + } + + response.writeHead(200); + response.write(file, "binary"); + response.end(); + }); + }); +} + +exports.wsRequest = wsRequest; diff --git a/parts/footer b/parts/footer new file mode 100644 index 0000000..f85901a --- /dev/null +++ b/parts/footer @@ -0,0 +1,5 @@ + +

+Copyright PJR.cc 2012 + + diff --git a/parts/header b/parts/header new file mode 100644 index 0000000..f1c3ea6 --- /dev/null +++ b/parts/header @@ -0,0 +1,5 @@ + + +Title! + + diff --git a/parts/middle b/parts/middle new file mode 100644 index 0000000..6530b63 --- /dev/null +++ b/parts/middle @@ -0,0 +1 @@ +Hi there diff --git a/res/favicon.ico b/res/favicon.ico new file mode 100644 index 0000000..3a79aca Binary files /dev/null and b/res/favicon.ico differ diff --git a/res/test.html b/res/test.html new file mode 100644 index 0000000..732eed5 --- /dev/null +++ b/res/test.html @@ -0,0 +1,3 @@ +

this is a test

+ +this is a journey, into sound...