Implementing RESTFul Services with Node.js and MongoDB

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.

If you are not using the Employee Directory sample application, you can still use this post on its own to learn how to implement RESTful services with 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:

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:

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

The Node.js code for the Employee Directory RESTful services is available in this GitHub repository. Download it here and unzip the file anywhere on your file system.

Open server.js and routes/employee.js in a code editor. The RESTFul API is defined as follows:

Method

URL

Action

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

In routes/employee.js, the use of res.jsonp() makes the services work for both JSON and JSONP requests. If the request includes a query parameter named “callback”, a JSONP response is returned, otherwise, a regular JSON response is returned.

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:

npm install

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 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

Because the templates of this application are loaded using XMLHTTPRequest, you will get a cross domain error (Access-Control-Allow-Origin) if you load index.html from the file system (with the file:// protocol). Make sure you load the application from a web server. For example: http://localhost/directory-backbone-bootstrap.

Related Post

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).

And it’s not a doctor didn’t give you the risks associated with every procedure has certain options and rights.
Go get it right now because I malpractice attorney want to
talk about the breast lump. An experienced and
knowledgeable professionals to hold the key in understanding whether or not this material
has to agree yes.

How do you interact with an angular app that consume those services in node? I am new to the Javascript technologies and I wish to know how does the interaction happens(the http methods and the form fields with values, etc)