Sysadmin by day, developer by night

Hi visitors from Reddit. Someone posted a link to a post over a year old. Don’t bother reading this. I’ve long since gone back to Tornado and enough has changed in both projects that any code below probably doesn’t apply.

I got pretty far into my primary project with Tornado. However, for the past couple months I’ve been really impressed with what I’ve been reading about node.js. The community for node.js is pretty amazing, much more active than what I’ve seen for Tornado.

I got to a point with Tornado where I felt like I was struggling to fit within the parameters of the framework, and the framework was really the only thing keeping me from trying out node.js. So, I’ve gone ahead and made the swap.

I’m not very far into my app. I’m now working on the same thing I always seem to do when working with a new framework. I’m writing up my own sessions implementation. I’ve just completed a basic url dispatcher, and what I’m learning is javascript is something new.

Going from Python to Javascript isn’t exactly easy. It requires thinking about things differently. I originally was trying to create base classes to handle requests, similar to Tornado’s RequestHandler. I got really far into, lots of code written, and it was really turning into quite a mess. So I stopped and thought about it, and realized I was trying to do it the Python way, not the Javascript way.

Here’s how I’m doing it now:


var sys = require('sys'),
http = require('http'),
url = require('url'),
utils = require('utils'),


var _404Handler = function(req, res, url){
res.writeHeader(404, {“Content-Type”: “text/plain”});
res.write(‘404’);
res.close();
}

var MainHandler = {
GET: function(req, res, url) {
res.writeHeader(200, {“Content-Type”: “text/plain”});
var session = new sessions.Session(req, res);
res.write(‘Main Handler’);
res.close();
}
}

var TestHandler = {
GET: function(req, res, url) {
res.writeHeader(200, {“Content-Type”: “text/html”});
res.write(‘Test Handler >form method=”POST”<>input type=”submit” value=”test post” /<>/form<’);
res.close();
},
POST: function(req, res, url) {
res.writeHeader(200, {“Content-Type”: “text/html”});
res.write(‘Test Handler post’);
res.close();
}
}


var routes = [
[/^\/$/, MainHandler],
[/^\/test\/*$/i, TestHandler]
];


http.createServer(function (req, res) {
var u = url.parse(req.url);
// basic logging
utils.log(req.url);
matched = false;
for (route in routes) {
if (u.pathname.match(routes[route][0])) {
matched = true;
if (req.method in routes[route][1]) {
routes[route][1][req.method](req, res, u);
} else {
_404Handler(req, res, u);
}
break;
}
}
// if we get to this point, no route matched
// the url, so return a 404
if (!matched) {
_404Handler(req, res, u);
}
}).listen(8000);
utils.log(‘Server running at http://127.0.0.1:8000/’);

It’s nice, simple, and gets the job done quickly. Right now I’m trying to figure out how best to handle cookies. I’ve already found a memcache interface for node.js, so I should be all set for sessions soon.

blog comments powered by Disqus
Technorati Profile