Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don’t worry, you can still create the pull request.
  • 9 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Showing with 91 additions and 42 deletions.
  1. +3 −2 examples/app.js
  2. +87 −39 lib/jqNode.js
  3. +1 −1 package.json
View
5 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'});
View
126 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("<h1>404. Not found.</h1>");
+ 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("<h1>404. Not found.</h1>");
+ }
}
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("<h1>Unable to load page. File not found</h1>");
- if(_debug) {
- console.log(fileName + " not found");
- }
+ _response.writeHead(404, {'Content-Type' : 'text/html'});
+ _response.end("<h1>404. Not found</h1>");
+ $.debug(fileName + " not found");
} else {
+ _response.writeHead(200, {'Content-Type' : contentType});
_response.end(data);
}
});
}
exports.$ = $;
+
View
2 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 <jpradeek@gmail.com>",
"keywords": ["framework", "jquery", "web"],
"repository": "git://github.com/pradeek/jqNode",

No commit comments for this range

Something went wrong with that request. Please try again.