In my previous post, I shared a new version of the Employee Directory sample application. By default, the application uses an in-memory data store to provide a “download and run” experience (no need to set up a server or a database). In this post, I’ll show how to replace the in-memory data store with RESTful services powered by Node.js, MongoDB and the Express framework.
Step 1: Install Node.js
If it’s not already installed on your system, follow the steps below to install Node.js:
- Go to http://nodejs.org, and click the Install button.
- Run the installer that you just downloaded. When the installer completes, a message indicates that Node was installed at /usr/local/bin/node and npm was installed at /usr/local/bin/npm. At this point node.js is ready to use.
Step 2: Install MongoDB
If MongoDB is not already installed on your system, refer to the MongoDB QuickStart for platform-specific installation instructions. Here are some quick steps to install MongoDB on a Mac:
- Go to http://www.mongodb.org/downloads and download the latest OS X 64-bit production release (2.4.2 at the time of this writing).
- In Finder, double-click mongodb-osx-x86_64-2.4.2.tgz to extract its contents. (Alternatively you can use: tar -zxvf mongodb-osx-x86_64-2.4.2.tgz)
- Move the mongodb-osx-x86_64-2.4.2 folder to /usr/local (or another folder according to your personal preferences):
sudo mv -n mongodb-osx-x86_64-2.4.2/ /usr/local/
- (Optional) Create a symbolic link to make it easier to access:
sudo ln -s /usr/local/mongodb-osx-x86_64-2.4.2 /usr/local/mongodb
- Create a folder for MongoDB’s data and set the appropriate permissions:
sudo mkdir -p /data/db sudo chown `id -u` /data/db
- Start mongodb
cd /usr/local/mongodb ./bin/mongod
Step 3: Download the Code
Open server.js and routes/employee.js in a code editor. The RESTFul API is defined as follows:
|GET||/employees||Retrieve all employees|
|GET||/employees?name=j||Retrieve employees whose name includes the letter “j”|
|GET||/employees/3||Retrieve employee with id=3|
|GET||/employees/3/reports||Retrieve employees that are direct reports of employee number 3|
Step 4: Install dependencies (Express Framework and MongoDB driver)
Open package.json in a code editor and note that the application has two dependencies: the Express framework and the MongoDB driver. To install these dependencies, open a command line (terminal window), cd to the directory where you extracted the source code, and type:
Step 5: Start the server
Make sure MongoDB is started (Step 2 point 6 above) before you start your Node.js server as follows:
Step 6: Testing the API in your Browser
- Retrieve all employees
- Retrieve all employees whose name includes the letter “j”
- Retrieve employee with id == 1
- Retrieve employees that are direct reports of employee number 1
Step 7: Test the API with the Employee Directory Client Application
- Download the source code for the Employee Directory client application available in this repository.
- Unzip the file anywhere on your file system.
- Open index.html in your code editor.
- Comment out the “model-in-memory.js” script, and uncomment the “model-jsonp.js” script to use the JSONP adapter that connects to your Node.js server.
- Make sure the MongoDB and Node.js servers are started
- Open index.html in a Browser and test the application
Check out this post for another Node.js example with a more complete RESTful API including the ability to add, modify and delete items (POST, PUT, and DELETE HTTP methods).