Created
June 19, 2010 21:10
-
-
Save Yuffster/445279 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sys = require('sys'); | |
var http = require('http'); | |
var url = require('url'); | |
require('./lib/MooTools'); | |
var Controllers = { | |
helloworld: { | |
echo: function(params,extraPath) { | |
return extraPath.capitalize(); | |
} | |
} | |
}; | |
var RequestHandler = { | |
routeRequest: function(path, params) { | |
var path = path || ""; | |
var path = path.replace(/^\//, ''); | |
var split = path.split('/'); | |
var controller = split[0]; | |
var command = split[1]; | |
var extraPath = split.slice(2).join('/'); | |
var response = ''; | |
if (path.contains('.')) { | |
response = 'File not found: '+path; | |
} else if (!Controllers[controller]) { | |
sys.puts("Can't find controller."); | |
response = "Controller not found: "+controller; | |
} else { | |
sys.puts("Routing to controller object."); | |
response = Controllers[controller][command](params, extraPath); | |
} | |
sys.puts("GET: "+path); | |
sys.puts("Controller is "+controller+", command is "+command+"."); | |
sys.puts("Response is: "+response); | |
if (!response) response = 'no response'; | |
return { | |
body: response | |
}; | |
} | |
}; | |
http.createServer(function(request, result) { | |
var params = url.parse(request.url, true); | |
var response = RequestHandler.routeRequest(params.pathname, params.query); | |
if (!response) response = {}; | |
if (!response.body) response.body = ':('; | |
result.writeHead(200, { | |
'Content-Type': 'text/plain', | |
'Content-Length': response.body.length | |
}); | |
result.write(response.body); | |
result.end(); | |
}).listen(8000); | |
sys.puts("Now listening on port 8000."); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment