1 var url = require("url");
2 var path = require("path");
3 var fs = require("fs");
4 var myparse = require("./myparse.js");
5 var webmain = require("./webmain.js");
7 //var staticExtensions = ["html", "gif", "jpg", "css", "js", "ico"];
9 function wsRequest(request, response) {
11 var purl = url.parse(request.url);
13 /*staticExtensions.forEach(function testExtn(setest) {
14 console.log("testing url: ", request.url);
15 console.log("against: ", purl.pathname);
16 var chk = purl.pathname.split(".");
17 console.log("chk: ", chk);
18 var chkid = staticExtensions.indexOf(chk[chk.length-1].toLowerCase());
19 console.log("chkid is ", chkid);
25 // if the end of the pathname is something.something, we assume static
26 var lpath = purl.pathname.split("/").pop();
27 var idx = lpath.indexOf(".");
28 console.log("testing url: ", request.url);
29 console.log("against: ", purl.pathname);
30 console.log("lpath is: '%s'", lpath);
31 console.log("type is: '%s'", typeof lpath);
32 console.log("idx is: ", idx);
34 if(idx > 0) isStatic = 1;
38 console.log("Service as static");
39 serveStatic(lpath, response);
43 //console.log("request: ", request);
44 console.log("purl: ", purl);
46 // now we need to find the extension used
47 // to serve the request based no the first purl
48 if(purl.pathname == "/") {
49 console.log("Serv main");
50 webmain.serveMain(request, response, function (request, response) {
56 var thispurl = purl.pathname.split("/")[1];
57 fs.stat("./purls/web_"+thispurl+".js", function (err, stats) {
58 console.log("get purl is "+thispurl+" and err "+err+" and "+stats);
60 response.writeHead(404, {"Content-Type": "text/plain"});
61 response.write("404 Not Found\n");
65 var thiserv = require("../purls/web_"+thispurl+".js");
66 if(thiserv.requireBody()) {
67 console.log("yubber is true");
68 webmain.serveBody(request, response, thiserv.process);
70 console.log("yubber is false");
71 thiserv.process(request, response, function(request, response) {
82 function serveStatic(staticname, response) {
83 var pathName = "./res/"+staticname;
84 console.log("Pathname for check is ", pathName);
85 fs.exists(pathName, function(exists) {
87 response.writeHead(404, {"Content-Type": "text/plain"});
88 response.write("404 Not Found\n");
93 fs.readFile(pathName, "binary", function(err, file) {
95 response.writeHead(500, {"Content-Type": "text/plain"});
96 response.write(err + "\n");
101 response.writeHead(200);
102 response.write(file, "binary");
108 exports.wsRequest = wsRequest;