diff --git a/examples/app.js b/examples/app.js
index 75ad178..d618dc0 100644
--- a/examples/app.js
+++ b/examples/app.js
@@ -4,8 +4,9 @@ $("/").get(function(request, response) {
$.writeFile("index.html");
});
-$("/test").post(function(request, response, data) {
+$(new RegExp("/test")).post(function(request, response, querystring, data) {
console.log(data);
+ $.write(data.sample);
});
-$.start(8888, true);
+$.start({debug: true, docroot: __dirname + '/htdocs'});
diff --git a/lib/jqNode.js b/lib/jqNode.js
index dbdc6ca..e7fdc7a 100644
--- a/lib/jqNode.js
+++ b/lib/jqNode.js
@@ -3,49 +3,69 @@ var fs = require("fs"),
url = require("url");
var routes = {},
+regexroutes = [],
server = http.createServer();
-var _response, _data, _debug = true;
+var _response, _data;
function route(request, response) {
var parsedUrl = url.parse(request.url, true),
pathName = parsedUrl.pathname,
method = request.method;
-
+
_response = response;
- _data = parsedUrl.query;
+ _querystring = parsedUrl.query;
- if(_debug) {
- console.log("Received " + method + " request at " + pathName);
- }
+ $.debug("Received " + method + " request at " + pathName);
+ $.debug(request.headers);
+
+ var handler = false;
- if(pathName === "/favicon.ico") {
- $.writeFile("favicon.ico", "image/x-icon");
+ if(routes[pathName] && (handler = routes[pathName][method])){
+ handler = routes[pathName][method];
} else {
- if(handler = routes[pathName][method]){
- if(method === "POST") {
- _data = "";
- request.addListener("data", function(chunk) {
- _data += chunk;
- });
- request.addListener("end", function() {
- handler(request, response, require('querystring').parse(_data));
- });
- } else {
- handler(request, response, _data);
- }
- } else {
- response.writeHead(200, {'Content-Type' : 'text/html'});
- response.end("
404. Not found.
");
+ for(routeindex in regexroutes) {
+ route = regexroutes[routeindex];
+ if(pathName.match(route.regex) && method == route.method) {
+ handler = route.callback;
+ break;
+ }
}
}
+ if(!handler && $.config.docroot) {
+ filename = $.config.docroot + pathName.replace(/\.\./, '.');
+ handler = function(){$.writeFile(filename, $.mimetype(filename));}
+ }
+ if(handler) {
+ _data = "";
+ request.addListener("data", function(chunk) {
+ _data += chunk;
+ });
+ request.addListener("end", function() {
+ handler(request, response, _querystring, require('querystring').parse(_data));
+ });
+ }
+ else {
+ response.writeHead(404, {'Content-Type' : 'text/html'});
+ response.end("404. Not found.
");
+ }
}
function addRoute(url, method, callback) {
- if(!routes[url]) {
- routes[url] = {};
+ finder = /function (.+?)\(/;
+ url.constructor.toString().match(finder);
+ switch(RegExp.$1) {
+ case "RegExp":
+ regexroutes.push({'regex': url, 'method': method, 'callback': callback});
+ break;
+ default:
+
+ if(!routes[url]) {
+ routes[url] = {};
+ }
+ routes[url][method] = callback;
+ break;
}
- routes[url][method] = callback;
}
var $ = function(url) {
@@ -83,17 +103,45 @@ $.fn['delete'] = function(callback) {
addRoute(this.url, "DELETE", callback);
}
+$.mimetypes = {
+ html: 'text/html',
+ htm: 'text/html',
+ png: 'image/png',
+ jpg: 'image/jpeg',
+ jpeg: 'image/jpeg',
+ gif: 'image/gif',
+ css: 'text/css',
+ js: 'text/javascript'
+};
+
+$.mimetype = function(filename) {
+ filename.match(/\.([^.]+)$/);
+ if($.mimetypes[RegExp.$1] != undefined) {
+ return $.mimetypes[RegExp.$1];
+ }
+ return 'application/octet-stream';
+}
-$.start = function(port, debugMode) {
- if(!port) {
- port = 8888;
+$.debug = function() {
+ if($.config.debug) {
+ console.log.apply($, arguments);
}
- server.on('request', route);
- server.listen(port);
- if(debugMode) {
- _debug = true;
- console.log("Listening at port " + port);
+}
+
+$.start = function(config) {
+ $.config = {
+ port: 8888,
+ debug: false,
+ docroot: false,
+ }
+ for (attrname in config) { $.config[attrname] = config[attrname]; }
+ if($.config.debug) {
+ console.log('Config Options:');
+ for (attrname in $.config) { console.log(' ' + attrname + ': ' + $.config[attrname]); }
}
+ server.on('request', route);
+ server.listen($.config.port);
+ $.debug("Listening at port " + $.config.port);
return server;
}
@@ -109,17 +157,17 @@ $.writeFile = function(fileName, contentType) {
if(!contentType) {
contentType = "text/html";
}
- _response.writeHead(200, {'Content-Type' : contentType});
fs.readFile(fileName, function(error, data) {
if(error) {
- _response.end("Unable to load page. File not found
");
- if(_debug) {
- console.log(fileName + " not found");
- }
+ _response.writeHead(404, {'Content-Type' : 'text/html'});
+ _response.end("404. Not found
");
+ $.debug(fileName + " not found");
} else {
+ _response.writeHead(200, {'Content-Type' : contentType});
_response.end(data);
}
});
}
exports.$ = $;
+
diff --git a/package.json b/package.json
index c44261b..a3b1caa 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
{
"name": "jqNode",
"description": "An easy to use jQuery-esque library for NodeJS",
- "version": "0.0.1",
+ "version": "0.0.2",
"author": "Pradeek ",
"keywords": ["framework", "jquery", "web"],
"repository": "git://github.com/pradeek/jqNode",