-
-
Save jeffrafter/353700 to your computer and use it in GitHub Desktop.
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]); | |
} | |
} |
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(); | |
}); | |
} | |
} | |
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'); |
This is awesome dude.
can someone explain the security hole part? I don't understand why reading the file locally is a bad thing
(btw, I am rewriting this as ES6 and will share when done)
@thevaleriemack, there is a type of attack called "Path/Directory traversal" that can be run against the server easily and the attacker can get all your files.You can read more about it here
Created 9 years ago, works with small modifications now : ) Thank you @jeffrafter
thank you @mkanzit!
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.
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.
Express in several tests performs 3x, 4x times slower than raw node. If you need performance a server like this it's the best choose.