Created
October 11, 2014 21:18
-
-
Save RadoRado/affde79b108b0b909a5e to your computer and use it in GitHub Desktop.
Minimal NodeJS HTTP router, made for an example. Kudos to @asotirov
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
/* global module, console */ | |
module.exports = function(http, port) { | |
'use strict'; | |
var routes = {}; | |
var exportMethods = {}; | |
var middlewares = []; | |
['get', 'post', 'put', 'delete'].forEach(function(method) { | |
routes[method] = routes[method] || {}; | |
exportMethods[method] = (function(method) { | |
return function(url, callback) { | |
return route(method, url, callback); | |
}; | |
}(method)); | |
}); | |
var server = http.createServer(function(req, res) { | |
var requestedUrl = req.url; | |
var requestedMethod = req.method.toLowerCase(); | |
var routeHandler = routes[requestedMethod]; | |
if(!routeHandler) { | |
throw new Error('Cannot handle method: ' + requestedMethod); | |
} | |
var handled = false; | |
Object.keys(routeHandler).forEach(function(url) { | |
if(requestedUrl === url) { | |
handled = true; | |
routeHandler[url].forEach(function(callback) { | |
invoke(req, res, callback); | |
}); | |
} | |
}); | |
if(!handled) { | |
res.writeHead(404); | |
res.end("Not found"); | |
} | |
}); | |
function invoke(req, res, callback) { | |
invokeMiddlewares(req, res, middlewares, callback); | |
} | |
function invokeMiddlewares(req, res, middlewares, callback) { | |
var index = 0; | |
function next() { | |
if(index === middlewares.length) { | |
callback(req, res); | |
} else { | |
console.log('Executing middleware ' + index); | |
middlewares[index++](req, res, next); | |
} | |
} | |
next(); | |
} | |
function use (middleware) { | |
middlewares.push(middleware); | |
} | |
function route(method, url, callback) { | |
var routesForMethod = routes[method]; | |
if(!routesForMethod) { | |
throw new Error('Cannot handle method: ' + method); | |
} | |
routesForMethod[url] = routesForMethod[url] || []; | |
routesForMethod[url].push(callback); | |
} | |
exportMethods.listen = server.listen.bind(server); | |
exportMethods.use = use; | |
return exportMethods; | |
}; |
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"), | |
router = require("./router")(http); | |
router.use(function(req, res, next) { | |
var data = ''; | |
req.on('data', function(chunk){ | |
data+=chunk.toString(); | |
}); | |
req.on('end', function() { | |
req.data = data; | |
try { | |
var body = JSON.parse(data); | |
req.body=body; | |
next(); | |
} catch(e) { | |
next('Invalid JSON body.'); | |
} | |
}); | |
}); | |
router.get('/panda', function(req, res) { | |
res.end('Pandaaaaaa!'); | |
}); | |
router.post("/panda", function(req, res) { | |
console.log(req.data, req.body); | |
res.end("PANDALISM"); | |
}); | |
router.listen(8081); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment