Created
April 2, 2010 20:59
-
-
Save jeffrafter/353700 to your computer and use it in GitHub Desktop.
Simple HTTP Server and Router in node.js
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
exports.createHandler = function (method) { | |
return new Handler(method); | |
} | |
Handler = function(method) { | |
this.process = function(req, res) { | |
params = null; | |
return method.apply(this, [req, res, params]); | |
} | |
} |
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 handlerFactory = require('./handler'); | |
var fs = require('fs'); | |
var sys = require('sys'); | |
var parser = require('url'); | |
var handlers = {}; | |
exports.clear = function() { | |
handlers = {}; | |
} | |
exports.register = function(url, method) { | |
handlers[url] = handlerFactory.createHandler(method); | |
} | |
exports.route = function(req) { | |
url = parser.parse(req.url, true); | |
var handler = handlers[url.pathname]; | |
if (!handler) handler = this.missing(req) | |
return handler; | |
} | |
exports.missing = function(req) { | |
// Try to read the file locally, this is a security hole, yo /../../etc/passwd | |
var url = parser.parse(req.url, true); | |
var path = __dirname + "/public" + url.pathname | |
try { | |
data = fs.readFileSync(path); | |
mime = req.headers.accepts || 'text/html' | |
return handlerFactory.createHandler(function(req, res) { | |
res.writeHead(200, {'Content-Type': mime}); | |
res.write(data); | |
res.close(); | |
}); | |
} catch (e) { | |
return handlerFactory.createHandler(function(req, res) { | |
res.writeHead(404, {'Content-Type': 'text/plain'}); | |
res.write("No route registered for " + url.pathname); | |
res.close(); | |
}); | |
} | |
} | |
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 sys = require('sys'); | |
var http = require('http'); | |
var router = require('./router'); | |
// Handle your routes here, put static pages in ./public and they will server | |
router.register('/', function(req, res) { | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
res.write('Hello World'); | |
res.close(); | |
}); | |
// We need a server which relies on our router | |
var server = http.createServer(function (req, res) { | |
handler = router.route(req); | |
handler.process(req, res); | |
}); | |
// Start it up | |
server.listen(8000); | |
sys.puts('Server running'); |
Cool. Se need more of this lindo of native implementations!
This is great. I developed an implementation of a NodeJS http router here: https://github.com/ChukwuEmekaAjah/http-router
However, I haven't figured out how to handle static file handling. Great job mate.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank god for stuff staying here forever. Years later and still relevant to some of us. I have a need for a project that require me to use just base packages for a small device. If you follow to my repo you will see the first version of it with the JS server. I am redoing it with more features and still node but using typescript to clean it up. I been looking for other ways to refactor it and this routing example is awesome. I will be borrowing a large portion of this refactoring template, maybe redoing a little to make it more tsc like. Awesome share.