A Guide to build a RESTful API using Node and MongoDB


 

MongoDB is an open-source document database and leading NoSQL database. MongoDB is written in C++. This tutorial will give you great understanding on MongoDB concepts needed to create and deploy a highly scalable and performance-oriented database.
In this tutorial we are going to learn following things.
  • Setting up MongoDB
  • Installing NPM modules.
  • Setting up our Server.
  • Creating basic MongoDB model.
  • Creating basic RESTful API’s using Node.js and MongoDB.
Seting up MongoDB

Visit official MongoDB download page to download MongoDB installer for specific operating system. They will detect your OS and offer you stable version to download. Once you have download it, just follow the setup wizard instructions to install it in your system.

To begin using the MongoDB, you need to specify the location where MongoDB will store your databases. That location will be a normal folder. Create any folder say “mongoData” at location easily accessible to you say on Desktop and open up terminal.
Open up terminal and run following command to start MongoDB Server at default port.


Then, open up another terminal and run following command.
To create new database, run following command.

Inside database, you create collections which actually like a table in database that will store your information. We will do all that using code. Let’s move to next step !

Installing NPM modules.

MongoDB provide native driver for Node.js called “mongodb” which you can use to connect to MongoDB and perform various CRUD operation. There is another MongoDB recommended node module which is quite famous called “mongoose”and this one we are going to use.

Create project folder and start your project by using “npm init” cause its good practice. This wizard will ask you some question like name, version, test script etc and at the end you will have your package.json file ready.

To install mongoose run following command.




Create your package file and run following command to install the modules.

This is it, we have bootstrapped our project. Let’s write some code.

Setting up our Server.

To set up our project Server, we are going to use Express module. Here is our basic server.

Let’s run our code and see how it’s behaving. Run the code using following command.





All right. We have set up our Server successfully. Moving right along !

Creating basic MongoDB model.

Mongoose allows you to create models ( OR Schema OR tables ) in your MongoDB database. To connect mongoDB database to our Node.js here is two-line of code.

Unlike SQL queries, MongoDB uses JSON structure to create schema in model. So for example if you want to create simple table user_login with two column say email and password you need to write following code

Create separate folder called “model” and inside create file named “mongo.js” and add following line of code.

In Server.js add following line.

Creating RESTful API’s using Node and MongoDB.

Let’s develop simple RESTful engine using MongoDB.
So our Resource will be “users” and on that we are going to allow following CRUD operation.

  • GET /users – Return all Users from MongoDB
  • POST /users – Add new user in MongoDB
  • GET /users/:id – Return User with matched ID
  • PUT /users/:id – Update users information
  • DELETE /users/:id – Delete particular user

1 : GET /users – Return all Users from MongoDB.

Here is our code.

To test the API, open up POSTMAN and hit “localhost:3000/users”

2 : POST /users – Add new user in MongoDB.

Here is a code.
















3: GET /users/:id – Return User with matched ID.

Here is our code.
















To check this route, hit our first API to get all users and copy any one ID. Then just paste the ID right after the URL and hit the request.

4 : PUT /users/:id – Update users information.

Here is our code.

In order to execute it, first get the ID of any user and the select type of request as PUT and pass data which you want to update in JSON format.

5 : DELETE /users/:id – Delete particular user.

Here is our code.





















Just select the request type as DELETE and pass the ID which user you want to delete.

Conclusion:
We have covered the information which you need to begin with MongoDB and Node.js along with RESTful api development tutorial. MongoDB is great in performing READ operation, so if you have a large amount of data say 1000TB then SQL is quite slow at that time. You can use big data software like MongoDB to handle that kind of record set.
Mongoose is wrapper over native Node.js driver provided by MongoDB. MongoDB official site recommends this module too.


Disqus Comments