Building Web APIs with Node.js and MongoDB
Node.js by itself is a blank canvas. However, the Express.js web framework for Node.js provides programmers with a clear, concise development platform to build high performance Web APIs. In this article, I’ll illustrate what it takes to build a simple Web API using Node.js and the Express.js web framework with MongoDB as the persistence mechanism. Under the hood I’ll use MongoDB and Mongoose to persist the data. NOTE: You can find the complete source code for this article at: https://github.com/donnfelker/workout-tracker The Node.js product is still in its infancy yet it’s already being touted in many tech circles as the quickest way to develop scalable APIs (and applications) that can handle a large load directly out of the box. The main benefit of Node.js is that any existing JavaScript knowledge you have instantly transfers over to Node.js as it is a JavaScript framework. While this is a net gain, the unfortunate consequence of a new framework like Node.js is that you’re left with a blank canvas to start your development from. Building a Web API from the ground floor is not only time consuming but it is also very error prone in the long run. Thankfully, Express, the high performance web framework for Node.js, exists. Express allows you to quickly create applications that have a majority of the common boilerplate code already written for you. In this article, you’ll learn how to use the Express web framework for Node.js by building a simple Web API for a sample application known as the Workout Tracker. Persistence in any Web API is the cornerstone of development. A common problem in API development is that the inserts or reads are far too slow due to the normalization of the data in a typical relational database. While there are ways to improve these queries (and insertions) with proper tuning of the database, one alternative up and coming approach is to use MongoDB as the backing store for APIs that require high throughput. Modeling the data in a document-oriented fashion (as MongoDB does) allows the developer to tackle the issue from a different angle - allowing the developer to store the data for the model in a complete entity in a MongoDB document. I’m not advocating that one approach is better than another, only that options from one approach might make more sense than another. In this article, I will show you how you can integrate MongoDB with Node.js to create a simple Web API using the Mongoose package. Standing on the Shoulders of Giants Node.js has not been around long - a mere three years - not even hitting an official version 1.0 (for this article I’ll use Node v0.8.0). During the last three years, various companies have started to adopt Node.js as part of their core business strategy. One of the alluring aspects of Node.js is the ability to share JavaScript between the client and the server. While many web developers already know JavaScript and want to learn Node.js, some are turned off at the thought of working with Node.js because there is not a web framework built into the core of Node.js. I was no exception. Upon my first glance at version v0.2.x of Node.js, I thought the feature set was somewhat bare and the landscape of available packages (and documentation and examples) was sparse to say the least. It was frustrating to see that I might have to spend a large amount of time building core components simply to be productive in this new environment. This was also of major concern to a lot of other developers (and sometimes still is). Most developers are concerned with how productive they will be. If you want a solution that makes working in Node.js easier, you’ll want to look at one of the few various web frameworks for Node.js. Some of those include Express, Flatiron and Geddy. In this article I will work with Express because Express is the most popular Node.js web framework at the moment. Creating Your First Express App In order to develop an Express application you must install Express first. To install Express, you need to have Node installed (I’m using v0.8.0 - get Node.js at nodejs.org). To install Express from the command prompt, type: npm install -g express
This command instructs NPM (Node Package Manager) to install Express. After a moment of some values streaming across the screen you will have installed Express! Using the -g switch instructs NPM to install the package globally inside of your current Node.js installation - meaning that you can access Express anywhere, not just in your current project. To create your first Express app, navigate to the destination where you’d like your application files to reside (e.g., /development or C:\development if you’re on Windows). Next, issue the following command from the command prompt: express ./workout-tracker
This will execute the express command that will create the Express application inside of a folder called workout-tracker. I will build this application during the course of this article. The Workout Tracker Express application is an API that will allow a user to keep track of the workouts they have performed through API commands. Once the command above finishes you will need to change directories and install the dependencies that the application needs. cd ./workout-tracker npm install -d
The -d switch on the npm install command informs npm to provide an information-level log output during the installation process of the dependencies that npm is installing. How does NPM know what the dependencies are? If no argument is supplied to the npm install command then npm will look for a file called package.json in the current directory and will read that file for all of the dependencies that need to be installed. Express created the package.json file during the express application generation process. The package.json file is very similar to the Gemfile featured in Rubygems in the Ruby language. The npm install command walks the package.json file and installs each of the dependencies. | & | | 
By: Donn Felker
Donn Felker is the Principal for Agilevent, a Microsoft Partner he founded in Minneapolis, Minnesota. He is an experienced Software Architect/Developer with over nine years of professional experience in various markets that include: entertainment, health, retail, insurance, financial, and real estate.
He is a Microsoft ASP Insider who currently holds certifications as an MCTS in Web Client Development for the ASP.NET 3.5 Framework, Certified Scrum Master and ITIL Foundation v2. He also holds a bachelors of science in Software Engineering. He writes, presents and consults on various topics ranging from architecture to agile practices to patterns and practices. He is the founder/coordinator of the Twin Cities Give Camp, President of the Twin Cities Developers Guild and the founder of Twin Cities Pragmatic Beer.
You can read Donn’s blog at http://blog.donnfelker.com
dfelker@gmail.com | Fast Facts | | Node.js and MongoDB both use JavaScript as the core language. If you build your front-end HTML client with tools like Backbone or Angular you will have a full stack application that shares its core language of JavaScript. | |
|