From d621aec05d8d430c199dfb72a547ae16dc8b59d4 Mon Sep 17 00:00:00 2001 From: paulr Date: Sun, 30 Dec 2012 21:43:13 +1100 Subject: [PATCH] added a method for setting resource path --- examples/simple/myapp.js | 10 ++++++++++ examples/simple/purls/main_footer.js | 7 ------- examples/simple/purls/main_head.js | 8 -------- examples/simple/purls/main_menu.js | 8 -------- examples/simple/purls/web_body.js | 8 -------- examples/simple/purls/web_nobody.js | 8 -------- examples/simple/purls/web_test.js | 11 ----------- lib/app.js | 8 ++++++++ lib/wsrequest.js | 7 ++++++- 9 files changed, 24 insertions(+), 51 deletions(-) delete mode 100644 examples/simple/purls/main_footer.js delete mode 100644 examples/simple/purls/main_head.js delete mode 100644 examples/simple/purls/main_menu.js delete mode 100644 examples/simple/purls/web_body.js delete mode 100644 examples/simple/purls/web_isbase.js delete mode 100644 examples/simple/purls/web_nobody.js delete mode 100644 examples/simple/purls/web_test.js diff --git a/examples/simple/myapp.js b/examples/simple/myapp.js index 7eed9ea..dd24da3 100644 --- a/examples/simple/myapp.js +++ b/examples/simple/myapp.js @@ -1,7 +1,17 @@ +// require the main njspurls modules var app = require("njspurls"); +// below we set a bunch of things globally for the app +// set the port to listen to app.setPort(8888); + +// location for loading "purls" app.setPurlPath("./purls"); + +// location for resources +app.setResPath("./res"); + +// purl to load for main page, i.e. / app.setDefaultMainPurl("main"); app.addMenu("name1", "/linkto1"); app.addMenu("name2", "/linkto2"); diff --git a/examples/simple/purls/main_footer.js b/examples/simple/purls/main_footer.js deleted file mode 100644 index 19f275e..0000000 --- a/examples/simple/purls/main_footer.js +++ /dev/null @@ -1,7 +0,0 @@ -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/examples/simple/purls/main_head.js b/examples/simple/purls/main_head.js deleted file mode 100644 index 463d8dd..0000000 --- a/examples/simple/purls/main_head.js +++ /dev/null @@ -1,8 +0,0 @@ -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/examples/simple/purls/main_menu.js b/examples/simple/purls/main_menu.js deleted file mode 100644 index 042b8a8..0000000 --- a/examples/simple/purls/main_menu.js +++ /dev/null @@ -1,8 +0,0 @@ -function go(request, response, callback) { - console.log("in main_head.go"); - response.write("
menuitem1item2item3
"); - callback(request, response); - return; -} - -exports.go = go; \ No newline at end of file diff --git a/examples/simple/purls/web_body.js b/examples/simple/purls/web_body.js deleted file mode 100644 index c9671ed..0000000 --- a/examples/simple/purls/web_body.js +++ /dev/null @@ -1,8 +0,0 @@ -function process(request, response, callback) { - response.write("Body"); - callback(request, response); -} - - -exports.process = process; -exports.requireBody = function() { return true;}; diff --git a/examples/simple/purls/web_isbase.js b/examples/simple/purls/web_isbase.js deleted file mode 100644 index e69de29..0000000 diff --git a/examples/simple/purls/web_nobody.js b/examples/simple/purls/web_nobody.js deleted file mode 100644 index 67eef07..0000000 --- a/examples/simple/purls/web_nobody.js +++ /dev/null @@ -1,8 +0,0 @@ -function process(request, response, callback) { - response.writeHead(302, { 'Location':'/body/'}) - callback(request, response); -} - - -exports.process = process; -exports.requireBody = function() { return false;}; diff --git a/examples/simple/purls/web_test.js b/examples/simple/purls/web_test.js deleted file mode 100644 index e746be5..0000000 --- a/examples/simple/purls/web_test.js +++ /dev/null @@ -1,11 +0,0 @@ -function requireBody() { - return true; -} - -function process(request, response, callback) { - response.write("from callback
"); - callback(request, response); -} - -exports.requireBody = requireBody; -exports.process = process; diff --git a/lib/app.js b/lib/app.js index 4fc59b0..7879fbc 100644 --- a/lib/app.js +++ b/lib/app.js @@ -26,6 +26,14 @@ exports.setPurlPath = function(setpath) { global.njspurls.mainPath = mainpath; } +exports.setResPath = function(setpath) { + // default purl path is set based on the module.parent if its relative + var basename = path.dirname(module.parent.filename); + var mainpath = path.resolve(basename, setpath); + console.log("setting res path to ", mainpath); + global.njspurls.mainResPath = mainpath; +} + exports.setFooterText = function(text) { global.njspurls.footerText = text; } diff --git a/lib/wsrequest.js b/lib/wsrequest.js index 20e71ba..6f7cb52 100644 --- a/lib/wsrequest.js +++ b/lib/wsrequest.js @@ -236,7 +236,12 @@ function resolveAndCall(request, response, caller, purlClass, callback) { } function serveStatic(staticname, response) { - var pathName = "./res/"+staticname; + var pathName = ""; + if(typeof global.njspurls.mainResPath != "undefined") { + pathName = global.njspurls.mainResPath + "/" + staticname; + } else { + pathName = "./res/"+staticname; + } console.log("Pathname for check is ", pathName); fs.exists(pathName, function(exists) { if(!exists) { -- 1.7.0.4