Skip to content

Instantly share code, notes, and snippets.

@gjohnson
Created September 30, 2011 09:15
Show Gist options
  • Save gjohnson/1253197 to your computer and use it in GitHub Desktop.
Save gjohnson/1253197 to your computer and use it in GitHub Desktop.
Simple HTTP Router Idea
var micro = require('./lib/micro');
micro.get('/json', function(req, res) {
res.json({
a: 1
, b: '2'
});
});
micro.get('/text', function(req, res) {
res.text('foooo!');
});
micro.get('/html', function(req, res) {
res.html('<h1>Hey!</h1>');
});
micro.listen(8000);
/*!
* Micro ~ Conceptual light weight HTTP router.
*
* @author Garrett Johnson
*/
var http = require('http')
, url = require('url')
, server = new http.Server()
, methods = ['GET', 'POST', 'PUT', 'DELETE']
, mappings = {}
, router = {}
, cache = {};
/**
* Expose `server`.
*/
exports = module.exports = server;
/**
* Route requests.
*/
server.on('request', onRequest);
/**
* Creates a routing function for a specific http verb.
*
* @param {String} http method
* @return {Function}
*/
function makeMethodRouter (method) {
return function (path, callback) {
var map = mappings[method];
map[path] = callback;
};
};
/**
* Iterate over supported verbs and create a bound routing function.
*/
methods.forEach(function(method){
exports[method.toLowerCase()] = router[method] = makeMethodRouter(method);
mappings[method] = {};
});
/**
* Http request callback. Looks up the callback for the pathname.
*
* @param {HttpServerRequest} request instance
* @param {HttpServerResponse} response instance
*/
function onRequest (req, res) {
var key = req.method + req.url
, cached = cache[key];
return (
cached ?
cached.call(null, req, res) :
(function(){
var parts = url.parse(req.url)
, route = cache[key] = mappings[req.method][parts.pathname] || function(req, res){
res.text('page not found', 404);
};
return route.call(null, req, res);
})()
);
};
/**
* Augments a simple `send` method for writing out the response.
*
* @param {String} body to write
* @param {Number} optional http status code, defaults to 200
* @param {String} optional content type, defaults to text/html
*/
http.ServerResponse.prototype.send = function(body, statusCode, contentType) {
this.statusCode = statusCode || 200;
this.setHeader('Content-Type', contentType);
this.setHeader('Content-Length', body.length);
this.end(body, 'utf8');
return this;
};
/**
* Augments a `json` method into the response
*
* @param {Object|Array} object to turn into json
* @param {Number} optional http status code, defaults to 200
*/
http.ServerResponse.prototype.json = function(obj, statusCode) {
this.send(JSON.stringify(obj), statusCode, 'application/json');
return this;
};
/**
* Augments a `html` method into the response
*
* @param {String} body to send
* @param {Number} optional http status code, defaults to 200
*/
http.ServerResponse.prototype.html = function(body, statusCode) {
this.send(body, statusCode, 'text/html');
return this;
};
/**
* Augments a `text` method into the response
*
* @param {String} body to send
* @param {Number} optional http status code, defaults to 200
*/
http.ServerResponse.prototype.text = function(body, statusCode) {
this.send(body, statusCode, 'text/plain');
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment