Created
February 9, 2011 05:20
-
-
Save samholmes/817929 to your computer and use it in GitHub Desktop.
My attempt in making a CodeIgniter routing server.
This file contains 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 http = require('http'); | |
var util = require('util'); | |
var path = require('path'); | |
var chromesMinimumByteFix = " "; | |
var server = http.createServer(function(req, res){ | |
var pathParts = req.url.split('/'); | |
pathParts.shift(); | |
var controllerName = pathParts[0] || 'index', | |
exportsName = pathParts[1] || 'index'; | |
var controllerPath = process.cwd() + '/controllers/' + controllerName + '.js'; | |
if (path.existsSync(controllerPath)) | |
{ | |
var controller = require(controllerPath); | |
if (typeof controller[exportsName] != 'undefined') | |
{ | |
// Ignore this here. theThis will contain API and be separated into another file. | |
var theThis = { | |
req: req, | |
res: res, | |
load: { | |
view: function(file, data) { | |
with (theThis.res) | |
{ | |
writeHead(200, {'content-type': 'text/html'}); | |
end('This is from the <em>load.view</em>'); | |
} | |
} | |
} | |
}; | |
controller[exportsName].call(theThis); | |
} | |
else | |
{ | |
res.writeHead(404, {'content-type': 'text/plain'}); | |
res.write("404 Error: Missing export function "+ exportsName +" in controller file located at " + controllerPath); | |
res.end(chromesMinimumByteFix);; | |
} | |
} | |
else | |
{ | |
res.writeHead(404, {'content-type': 'text/plain'}); | |
res.write("404 Error: Missing controller file located at " + controllerPath); | |
res.end(chromesMinimumByteFix); | |
} | |
}); | |
server.listen(80, 'localhost'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment