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);
27 var servethis = purl.pathname.split("/").pop();
28 serveStatic(servethis, response);
32 //console.log("request: ", request);
33 console.log("purl: ", purl);
35 // now we need to find the extension used
36 // to serve the request based no the first purl
37 if(purl.pathname == "/") {
38 console.log("Serv main");
39 webmain.serveMain(request, response, function (request, response) {
45 var thispurl = purl.pathname.split("/")[1];
46 fs.stat("./purls/web_"+thispurl+".js", function (err, stats) {
47 console.log("get purl is "+thispurl+" and err "+err+" and "+stats);
49 response.writeHead(404, {"Content-Type": "text/plain"});
50 response.write("404 Not Found\n");
54 var thiserv = require("../purls/web_"+thispurl+".js");
55 if(thiserv.requireBody()) {
56 console.log("yubber is true");
57 webmain.serveBody(request, response, thiserv.process);
59 console.log("yubber is false");
60 thiserv.process(request, response, function(request, response) {
71 function serveStatic(staticname, response) {
72 var pathName = "./res/"+staticname;
73 console.log("Pathname for check is ", pathName);
74 fs.exists(pathName, function(exists) {
76 response.writeHead(404, {"Content-Type": "text/plain"});
77 response.write("404 Not Found\n");
82 fs.readFile(pathName, "binary", function(err, file) {
84 response.writeHead(500, {"Content-Type": "text/plain"});
85 response.write(err + "\n");
90 response.writeHead(200);
91 response.write(file, "binary");
97 exports.wsRequest = wsRequest;