Modifying NodeJS development to use a config file instead of symlinks 95/7795/1
authorAnirudh Ramachandran <anirudhvr@gmail.com>
Sat, 7 Jun 2014 01:56:54 +0000 (18:56 -0700)
committerAnirudh Ramachandran <anirudhvr@gmail.com>
Sat, 7 Jun 2014 01:57:37 +0000 (18:57 -0700)
Change-Id: Iecc199e66c307f94dccdee300e612d3acfdf405c
Signed-off-by: Anirudh Ramachandran <anirudhvr@gmail.com>
node/config.js [new file with mode: 0644]
node/static.js

diff --git a/node/config.js b/node/config.js
new file mode 100644 (file)
index 0000000..5c0d5e1
--- /dev/null
@@ -0,0 +1,14 @@
+// Enter all relative paths to your app's files / directories here
+
+var appname = 'simpleappfoo';
+
+var Config = {
+ index: "../" + appname + "/src/main/resources/WEB-INF/jsp/main.jsp",
+ basepath:  "../web/src/main/resources/",  // common web bundle assets
+ apppath: "../" + appname + "/src/main/resources/", // app bundle assets
+ appdir_to_remove: appname + "/web",
+
+
+}
+
+module.exports = Config;
index 9411d1deb6442d877796bf698aae08576c982559..80806b7bdf9cccaf6c55ff72c358ab713c007be8 100644 (file)
@@ -1,37 +1,69 @@
 var url = require('url'),
-path = require('path'),
-fs = require('fs'),
-mime = require('mime');
+    path = require('path'),
+    fs = require('fs'),
+    mime = require('mime'),
+    config = require('./config');
+
+
+
+String.prototype.endsWith = function(suffix) {
+    return this.indexOf(suffix, this.length - suffix.length) !== -1;
+};
 
 var Static = function(request, response) {
-  var uri = url.parse(request.url).pathname, filename = path.join(process.cwd(), uri);
-  fs.exists(filename, function(exists) {
-    if(!exists) {
-      response.writeHead(404, {"Content-Type": "text/plain"});
-      response.write("404 Not Found\n");
-      response.end();
-      return;
+    var uri = url.parse(request.url).pathname;
+    var cwd =  process.cwd();
+    var found = false;
+
+    if (uri.endsWith("/")) {
+        if (uri == "/") {
+            found = true;
+            return returnFile(request, response, path.join(cwd, config.index), "text/html");
+        } else {
+            uri =  uri + "index.html";
+        }
     }
 
-    if (fs.statSync(filename).isDirectory()) filename += '/index.html';
+    var fn = "";
+    if (uri.indexOf("/" + config.appdir_to_remove) > -1) {
+        uri = uri.replace("/" + config.appdir_to_remove, "");
+        fn = path.join(path.join(cwd, config.apppath), uri);
+    } else {
+        fn = path.join(path.join(cwd, config.basepath), uri);
+    }
+
+    if (fs.existsSync(fn)) {
+        return returnFile(request, response, fn);
+    } else {
+
+        console.log("404 - " + uri);
+        response.writeHead(404, {"Content-Type": "text/plain"});
+        response.write("404 Not Found\n");
+        response.end();
+    }
+}
+
+function returnFile(request, response, filename, mimetype) {
+
+    var mimetyp = typeof mimetype !== 'undefined' ? mimetype : mime.lookup(filename);
 
     fs.readFile(filename, "binary", function(err, file) {
-      if(err) {
-        response.writeHead(500, {"Content-Type": "text/plain"});
-        response.write(err + "\n");
+        if(err) {
+            console.log("500 " + filename);
+            response.writeHead(500, {"Content-Type": "text/plain"});
+            response.write(err + "\n");
+            response.end();
+            return;
+        }
+
+        console.log("200 " + filename);
+        response.writeHead(200, {
+            "Content-Type": mimetyp,
+            "Access-Control-Allow-Origin": "*",
+        });
+        response.write(file, "binary");
         response.end();
-        return;
-      }
-
-      console.log(request.url);
-      response.writeHead(200, {
-        "Content-Type": mime.lookup(filename),
-        "Access-Control-Allow-Origin": "*",
-      });
-      response.write(file, "binary");
-      response.end();
     });
-  });
-}
 
+}
 module.exports = Static;