I have recently tried to code up a web-app using Node.js / MongoDB, and that adventure ended up rather unsuccessfully, after I decided that Node.js, while very cool, should not be the tool of choice if all you want to do is develop a standard web-app that doesn’t involve things such as high concurrency, or real time data flow between client and server. I have now started learning Ruby and the Ruby on Rails framework, and I don’t understand why I haven’t started using RoR earlier - it’s actually quite nice!
Now, I do not mean to say that Node.js is not an awesome web-server, it is, and I have got to know it quite well. My first adventure into Node.js was a multiplayer space game where you can fly a ship, go onto planets, and trade resources. Here is a screenshot:
Well, that took a couple thousand lines of code, and Node.js was a really good choice for it. Real-time bidirectional communications channel makes it very easy to send and receive messages about ship position, ship movement, etc. If you would like to read more about how I implemented the game, here is a pdf which goes into the details. The whole git repository is obtainable here.
After my successful usage of Node.js to make a multiplayer webgame, I decided it would be a good idea to use it for more standard web-app development that doesn’t call for real-time concurrency. That didn’t end up so well. I have spent considerable time doing things not directly related to the app, that took away time from proper app development. Here are some things that took lots of time…
The routing mechanism (even when using the great expressjs), didn’t seem to have a feature I wanted it to have, so I spent some time writing and releasing to npm a package to help with that. If you’re interested, it’s located here.
The callbacks. Oh, the callbacks. Considering node is single-threaded, the whole application flow is done with callbacks, which, for a complicated application with interspersed database calls, requires a fair amount of mental gymnastics, taking away time to work on the app logic. Just the registration / login code had around a depth of 6 of nested functions…
Reinventing the wheel. Pretty much everything has to be done from scratch, from the login system, to any type of what you could call an ORM.
Cohesiveness. This was actually the main reason why I abandoned the use of Node on my next standard web-app. All my code just seems to lack cohesiveness, however I structure it. One reason for this is lack of classes and object-orientation, which is partially solved using Coffeescripts classes, but still not a great solution. Along with all the callbacks, the code quickly becomes spaghetti. I have now written a Rails app with the same functionality as my Node app, the code exhibits much more cohesiveness, probably due to the fact that Rails uses convention over configuration.
Data-Access Layer. In my quest to NoSQL nirvana, I decided to use MongoDB with Node. One problem is that Node does not have any good ORM, and whatever there is, seems buggy, or I spend half an hour on IRC trying to get help on how to do something. I was using a node library for connecting to MongoDB, but there was some problem with the Map-Reduce bindings. Given my predisposition to have things perfect, I went on to spend a couple hours finding the bug and fixing it. I am happy to help out, but not just when I want to get my application working, and software maturity prevents me from doing it.
1 Ruby and Rails
After spaghetting myself in callbacks and spending way too much time modifying not-working Node packages, I ventured into Ruby and Ruby on Rails.
I have just started learning Ruby and RoR yesterday, and have already (!) managed to get a simple application working with Twitter authentication. Rails makes everything so simple, and I shiver when I look back at the hacky Coffeescript Node code, which is most likely filled with security holes.
Possibly this is just beginner’s excitement (I had the same when I started using Node after having made sites in PHP), but for now I am very pleasantly surprised by Rails, and by the fact I somehow never managed to use it before.
2 Conclusion
Node.js is awesome for one-page web-apps that require concurrency and real-time interaction, for example games. Or just have a look at this years Nodeknockout for some excellent examples of uses for Node! But please, do not try and use it when developing a web-app with many subpages, many models, and all that other stuff.
Additionally, I think I’m back to using more ‘earthly’ technologies. All the hype about Node and NoSQL made me get myself into technology that’s very new and still quite buggy. Though MongoDB is actually quite cool, it has awesome functionality such as geospatial indexing, and I’m sure I’ll be back to using it at one point!
Anyways, now I’m back to playing around with Ruby on Rails!