Skip to content

Instantly share code, notes, and snippets.

@jeffrafter
Created April 2, 2010 20:59
Show Gist options
  • Save jeffrafter/353700 to your computer and use it in GitHub Desktop.
Save jeffrafter/353700 to your computer and use it in GitHub Desktop.
Simple HTTP Server and Router in node.js
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');
@jeffrafter
Copy link
Author

This is a super simple setup for setting up route based handlers and serving local files. The content-type is wrong and the local file serving is a big security hole, so beware.

@matt212
Copy link

matt212 commented May 10, 2017

is there any es6 or current Node.js version of above codebase !

@collinsnji
Copy link

collinsnji commented Jun 13, 2017

This is good, but I recommend using Express.js for routing.

@wallysson-hotmart
Copy link

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.

@frayhan94
Copy link

This is awesome dude.

@v-stickykeys
Copy link

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)

@mkanzit
Copy link

mkanzit commented May 29, 2018

@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

@EvgEvg
Copy link

EvgEvg commented Aug 29, 2018

Created 9 years ago, works with small modifications now : ) Thank you @jeffrafter

@v-stickykeys
Copy link

thank you @mkanzit!

@PoSHMagiC0de
Copy link

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.

@LautaroJayat
Copy link

Cool. Se need more of this lindo of native implementations!

@ChukwuEmekaAjah
Copy link

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