20 Jan 2014

Create REST web service using NodeJS & MongoDB


In this article we will learn how to create a web application which has REST web services to create, get and delete user data.

First lets identify the different modules we are going to use for this example in NodeJS
a) Mongoskin - Module to connect and operate on MongoDB collection needed for this example
b) Restify - Module to offer basic operations / functionality needed to create REST endpoints and handlers

So lets first detail out how we are going to create a package for the npm for this project, here is how the package.json for this project would look like

  "name": "RestWebApp",
  "version": "0.1.0",
  "description": "RestWebApp",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified! Configure in package.json\" && exit 1"
  "repository": "",
  "keywords": [
  "dependencies": {
   "restify": "*",
   "mongoskin": "~0.6.0"
  "author": "Anup Gaurav",
  "license": "MIT",
  "readmeFilename": "README.md"
Important things to note here is that we have added both the needed dependencies in our "dependencies" section of the package.json. Fairly we can skip with specifying version for the modules, however i have defined the version for mongoskin.

In this example we will also try to cover briefly as to how to make the code / project structure modular, see the screen shot attached below
Figure 1: Project structure for the Rest application project
 If you see in detail the project structure, you will note one thing new, i.e. having the routes folder, what i am intending to do here is to make things modular, so i have created this file user.js which will have all the operations on the user collection rather than all being in the app.js. I will keep app.js just to initiate and have the REST endpoints defined within it. Lets lets first get started with user.js, where we will define all three data operations on the collection,
a) AddUser - To add a new user in the collection MyUsers for this application
b) GetUser - To get user document for a specific email id
c) RemoveUser - To delete user document from a collection matching a specific email id

Now let me put the contents of user.js file and then we will discuss the contents of the same

exports.addUser= function(req, res, next){
 if (!req.body || !req.body.userName) return next(new Error('No data provided.'));
  first_name: req.body.firstName,
  last_name: req.body.lastName,
  age: req.body.age,
  active: false
  }, function(error, appUser){
   if (error) return next(error);
   if (!appUser) return next(new Error('Failed to save.'));
   console.info('Added %s with id=%s', appUser.first_name, appUser._id);

exports.getUser= function(req, res, next){
 if (!req.params.email) return next(new Error('No input parameter provided.'));
  email:req.params.email}, function(error, appUser){
   if (error) return next(error);
   if (!appUser) return next(res.send(404));
   console.info('Found %s with email=%s', appUser.first_name, appUser.email);

exports.removeUser= function(req, res, next){
 if (!req.params.email) return next(new Error('No data provided.'));
  email:req.params.email}, function(error, appUser){
   if (error) return next(error);

Now lets understand this code better,  we have actually made some method definitions that needs to be exported (thats why we have used exports) where ever called.
Now each of the these function definitions start with function(req, res, next), where req is the http request object, res is http response object and next is a router control being offered by node js to call the next matching route when called.

Secondly you would question about the code within each function, we have actually called standard mongodb operations using mongodb standard functions, however there are few references that we have defined in the app.js and used it here, say how we have referenced the collection and DB on which we want to operated, in this file we have referenced it by req.db.appUser, but the same has been defined in the app.js file. When you would see the code of app.js, you would probably be able to relate.

Also to not is that we have used standard operators http request object req to get the either from the JSON object being passed in request body(In case of addUser) or request parameter being passed in the GET and DELETE operation.

Now lets also see the app.js code which would make the code complete and give us better clarity as to how the entire piece is working, here is the app.js code

var restify = require('restify');
var mongoskin = require('mongoskin');
var user = require('./routes/user');

// Server
var server = restify.createServer({
  name: 'myapp',
  version: '1.0.0'

//DB Connection
var db = mongoskin.db('mongodb://localhost:27017/RestApp?auto_reconnect', {safe:true});

//Define References
server.use(function(req, res, next) {
 req.db = {};
 req.db.appUser = db.collection('MyUsers');

//Define Parsers

// Define POST operation on AppUser Collection
server.post('/user', user.addUser);
server.get('/user/:email', user.getUser);
server.del('/user/:email', user.removeUser);

server.listen(80, function () {
  console.log('%s listening at %s', server.name, server.url);

Now lets go over this code line by line.

In the first two lines we have imported the required dependencies for mongoskin and restify modules.,
The third line of code has a import to the user module we created with the db function we have defined, moving along we have then created a restify server using the restify module that we imported above.
Further using the mongoskin module we connected to the mongoDB.

Now comes a very important section where we have asked the server to use some referenced such as
req.db.appUser = db.collection('MyUsers');

which states that when ever we call req.db.appUser, i actually mean to refer to the collection 'MyUsers' in the mongoDB connection i made above. Post this we have defined the parsing modules to be used by the server.

Now something very important and yet very simple using the restify module being imported. We have defined 3 endpoints , one for POST, one for GET and the last one for DELETE HTTP operations that we wanted to process. Point to note here is that we are using same/similar URL patterns of the request, but distinguishing them with the HTTP verb being used, which is how a REST web service end point should be coded and we have done the same way adhering to best practices for REST web services.

At last we start the server and start listening to port 80.
You can now go ahead and test the application using a REST client, please note that while testing the POST request, make sure you set the content-type as application/JSON in the header before sending the request.

Just test it out and you will have your first REST web services using NodeJS and MongoDB working.    

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.


  1. Kanhasoft is the top-notch Node.js development company in India. We have 45+ experienced programmers to delivered successful NodeJS Application development globally. Visit our site to know more about us.



© 2013 NimbleGeek. All rights resevered. Designed by Templateism

Back To Top