public
Created

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
// var sys = require('sys'),
// http = require('http'),
// url = require('url'),
// terminal = require('child_process').spawn('bash'),
// response,
// index;
 
 
/*
var createServer = function(){
 
http.createServer(function(req, res) {
 
var url_parts = url.parse(req.url);
var route = url_parts.pathname;
 
response = '';
 
res.writeHeader(200, {"Content-Type": "text/html"});
 
if(route == '/'){
console.log(route);
response = index;
res.end(response);
}
 
if(route == '/darken'){
console.log(route);
 
terminal.stdout.on('data', function (data) {
res.end(data);
return
});
 
terminal.stdin.write('echo -e "#main\n :color darken(#fff,9)" | ~/.gem/ruby/1.8/gems/sass-3.1.15/bin/sass -t compact');
terminal.stdin.end();
}
 
}).listen(8000);
 
}*/
 
/**
* Node.JS Sinatra Style Routing
*/
 
var routes = {}
, http = require('http')
, parse = require('url').parse
, server = http.createServer()
, fs = require('fs')
, sys = require('sys')
, terminal = require('child_process').spawn('bash');
 
/**
* @class http.ServerResponse
* @method send
* @param {Object/String} message Data to send
* @optional {Number} code HTTP Status Code
* @optionsal {String} type Content-Type
*/
 
http.ServerResponse.prototype.send = function(message, code, type) {
code = code || 200;
type = type || 'text/html';
// if (/object/.test(typeof(message))) {
// message = JSON.stringify(message);
// type = 'application/json';
// };
this.writeHead(code, { 'Content-Type': 'text/html' });
this.end(message);
};
 
/**
* @method addRoute
* @param {String} path URL endpoint
* @param {String} method HTTP Method
* @param {Function} fn Callback function
*/
 
function addRoute(path, method, fn) {
routes[path] = {};
routes[path][method] = fn;
};
 
/**
* @method route
* @param {Object} request HTTP Request object
* @param {Object} response HTTP Response object
*/
 
function route(request, response) {
var pathname = parse(request.url).pathname;
if (routes[pathname] && routes[pathname][request.method]) {
var handle = routes[pathname][request.method];
handle.apply(this, arguments);
} else {
};
};
 
['GET','PUT','POST','DELETE'].forEach(function(method) {
http.Server.prototype[method.toLowerCase()] = function(path, callback) {
addRoute(path, method, callback);
}
});
 
server.on('request', function theHandler(request, response) {
route(request, response);
});
 
/**
* Demo
*/
 
server.get('/', function(request, response) {
var k = fs.readFileSync('./index.html','utf8');
response.send( k );
});
 
server.get('/routes', function(request, response) {
response.send(routes);
});
 
server.listen(8000);
 
/* EOF */

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.