Permalink
Browse files

Moved stuff around...

  • Loading branch information...
1 parent e16da56 commit f21d85997b3e66de2a95394b611f4851d0dde1a6 @Poincare committed Dec 26, 2011
Showing with 118 additions and 2 deletions.
  1. +1 −0 README.md
  2. +83 −0 lib/Birbal.js
  3. +31 −0 lib/example.js
  4. +3 −2 package.json
View
@@ -3,6 +3,7 @@
A clever little framework for Node.js
* Handles simple routing for you
+* Handles POST and GET variables
* Doesn't restrict you at all.
Install with:
View
@@ -0,0 +1,83 @@
+var http = require("http");
+var url = require("url");
+var qs = require("querystring");
+
+var Birbal = function (port, routes) {
+
+ this.specials = {};
+
+ this.loadSpecials = function() {
+ for(var i in routes) {
+ if(i === ":before") {
+ this.specials["beforeFunc"] = routes[i][0];
+ }
+ else if(i === ":catchall") {
+ this.specials["catchAllFunc"] = routes[i][0];
+ }
+ }
+ }
+
+ this.onRequest = function (request, response, specials) {
+ var pathname = url.parse(request.url).pathname;
+
+ var request_vars;
+
+ if(request.method == 'POST') {
+ var body = ''
+ request.on('data', function(data) {
+ body += data;
+ });
+
+ request.on('end', function() {
+ request_vars = qs.parse(body);
+ });
+ }
+
+ else if(request.method == 'GET') {
+ var url_parts = url.parse(request.url, true);
+ var query = url_parts.query;
+
+ request_vars = query;
+ }
+
+ var matched = false;
+ for(var i in routes) {
+
+ if(pathname === i) {
+
+ if(routes[i][1] === request.method) {
+ response.writeHead(200, {"Content-Type":"text/html"});
+ response.write(routes[i][0](request_vars));
+
+ matched = true;
+
+ response.end();
+ break;
+ }
+ }
+
+ }
+
+ if(matched != true) {
+ if(this.specials["beforeFunc"] != null) {
+ this.specials["beforeFunc"]();
+ }
+
+ if(this.specials["catchAllFunc"] != null && this.specials["catchAllFunc"] != undefined) {
+ response.writeHead(200, {"Content-Type":"text/html"});
+ response.write(this.specials["catchAllFunc"](request_vars));
+ response.end();
+ }
+ }
+ }
+
+ this.start = function() {
+ this.loadSpecials();
+ http.createServer(this.onRequest.bind(this)).listen(port);
+ }
+
+ this.start();
+}
+
+
+exports.Birbal = Birbal;
View
@@ -0,0 +1,31 @@
+var serv = require("./Birbal.js");
+
+//called for "/" and "/else" for GET requests
+function hello() {
+ return "Wazzup!";
+}
+
+//called for "/misc" GET requests
+function misc() {
+ return "<h1>Misc</h1>";
+}
+
+//called before ever single request
+function before() {
+ console.log("Hey! A request came in!");
+}
+
+//called when no other request matches
+function oops() {
+ return "Oops!";
+}
+
+var routes = {
+ ":before":[before],
+ "/":[hello, "GET"],
+ "/else":[hello, "GET"],
+ "/misc":[misc, "GET"],
+ ":catchall":[oops, "GET"]
+};
+
+serv.Birbal(routes);
View
@@ -1,6 +1,6 @@
{
"name" : "Birbal",
- "version" : "0.0.1",
+ "version" : "0.0.3",
"description":"Quick, simple routing",
"keywords":["routing", "router", "framework"],
"author":"Dhaivat Pandya <dhaivatpandya@gmail.com> (http://poincare101.blogspot.com)",
@@ -12,6 +12,7 @@
{ "email" : "dhaivatpandya@gmail.com",
"url" : "http://github.com/poincare/Birbal/issues"
},
- "directories" : {"lib" : "./"}
+ "directories" : {"lib" : "./lib"},
+ "files" : ["./lib/Birbal.js"]
}

0 comments on commit f21d859

Please sign in to comment.