Building Web Apps with MongoDB, Node.js and Angular.js
I became an iPhone developer the moment I realized that the iPhone is not just a pretty and expensive phone; but rather that it is actually a networked Unix machine that fits in your pocket. The fact that it could (sometimes) make phone calls was incidental. From the very beginning it was a personal information device that I could use to find, consume and visualize all sorts of data.
I have at least two projects I want to build using this architecture. The first is a side project based around some ideas that interest me and the second may have more commercial value. Their details are not important right now though I plan on discussing the first at length in future posts as I learn more.
I've built prototypes for both of these project using Ruby on Rails (RoR) which is great and you should keep using it if you know and love it. In particular RoR is amazing because of the language, the ecosystem of gems and tools, and its explicit and implicit conventions, assumptions and opinions. However, I'm finding that RoR may not be a perfect fit for these new types of web apps. Let me explain some of my reasoning.
I built the two prototypes using MySQL and PostgreSQL. Those databases worked great and ActiveRecord/ActiveModel made creating the schemas easy. However, I'm drawn to the document model since it seems natural to work with data structures that contain or own other data structures rather than having to rebuild them from independent tables all the time. Document stores are flexible, can be fast in the right circumstances and I believe require less support from an ORM/ODM.
Choosing a document/NoSQL database is a different question as there are many choices (MongoDB, CouchDB, etc.) and various articles try to outline the differences. After looking them over MongoDB seems like a great default choice for me. Its fast, flexible and easy to query making it great to develop with. As I learn more and as my specific needs evolve I may swap out part or all of MongoDB but for now I could not be happier with it and with the support from 10Gen.
jQuery is great for DOM manipulation but highly interactive clients get complicated quickly and can benefit from the structure of an additional application framework. Lately BackBone.js is getting a lot of attention. I've also looked at SproutCore 2.0 and Batman.js and they too seem workable. However the one that felt easiest and cleanest is Angular.js.
Some things I like about it are that the "templates" are plain html, bindings and controllers are dead simple, it supports dependency injection, xhr requests are straight forward and it has a strong testing culture. Its development is being supported by Google and I expect to hear more from other developers soon. I suggest you check it out and let me know what you think.
I'm sure not all of these decisions will work out. I'll write more as I learn and figure things out but right now I plan to use:
- Node.js for the server
- MongoDB as the document/data store
- Mongoose.js as an Object Document Mapper and connection to MongoDB
- Express.js as a Sinatra like http framework
- Jade.js to provide templating for our very few other pages
- Less.js for enhancing CSS on the client
- Angular.js and jQuery for the client side app framework.
I've already started using this combination of tools and am very excited about the potential and what I can build.
Thats it for now. Thanks for letting me outline my thought process and describe my experience with these projects. Next time I'll explain more about the first project and we'll start building it.