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');
@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