NodeJS and MongoDB : Basic CRUD Rest API

In this tutorial, I will share how to create a simple To Do Application using NodeJS Rest API. We will perform the CRUD operation using MongoDB for our database. We also will be using mongoose, a javascript library to make us easier to work with MongoDB.

Installing MongoDB

  1. Go to https://www.mongodb.com/ and choose Product – Software – MongoDB Server and download the file according to your operating system.
  2. Move the file to your user directory, extract the downloaded file, rename the folder to ‘mongo’, and create another folder named ‘mongo-data’.
  3. Open your terminal command, go to the mongo/bin folder and type the following command to activate : ./mongod –dbpath ~/mongo-data/
  4. Open a new tab to try to put some commands. Type ./mongo 
  5. Try to input a command, for example: > db.Todos.insert({text:’Create Node Tutorial’})
  6. Fetch the data by typing this command : > db.Todos.find(). We will get an object that we have just created.
  7. Close the connection by typing Ctrl + C.

Installing Robo 3T (Robomongo)

  1. Download the installer from https://robomongo.org/
  2. Download Robo 3T and extract the compressed file.
  3. Connect to your local database :
    Robomongo
  4. The Todo collection that we’ve created before will appear. Robomongo 2

Collection in NoSQL is a table in SQL. Row and Record in SQL are called Document in NoSQL. Inside the SQL, we may call column. In NoSQL, we call it Field.

Connecting Node Application to MongoDB Database

  1. In your preferred directory (Desktop or Sites directory or else), create a new folder ‘node-todo-api’.
  2. Go inside the directory and initialize node application using the command: > npm init. Use the default setting using enter.
  3. Install mongodb : npm install [email protected] –save
  4. Create a new folder ‘testing’ for testing. and inside it create a new file mongodb-connect.js. Type node testing/mongodb-connect.js
  5. Open Robo3T, you will find the data that you’ve created : MongoDB Collection\

Mongoose ORM

Mongoose is a fantastic npm library. Mongoose makes it really easy to structure your data. We can perform validation of our data.

1. Install Mongoose

To install mongoose, type the following command:

2. Create server.js

Create a new folder ‘server’ and create a file named ‘server.js’ inside that directory.

3. Run the file

Use the command :

Your todos will be available then :

Todo App

Using Postman

1. Go to getpostman.com

Download postman for our api tools from https://getpostman.com and install it.

2. Open Postman and play around with it

postman

Add POST /todos Route

1. Install express and body-parser

To work with api, we will install two additional tools, they are express and body-parser. Open your terminal, go to your project directory and type the following command:

2. Refactor server.js

We will modify the server.js for better file management. At first, create a new folder ‘db’ inside server folder, and then create a mongoose.js file inside db directory :

mongoose.js

Next, inside the server folder, create another folder ‘models’ and create two files inside it. The todos.js and user.js

todos.js

user.js

Now, refactor server.js :

server.js

3. Create a new todo from Postman

Now that the files are ready, run your server by typing node server/server.js inside your project directory. Don’t forget to run your mongodb server (go to the mongo/bin folder and type the following command to activate : ./mongod –dbpath ~/mongo-data/).

Open Postman and type the endpoint : localhost:3000/todos. Open the Body tab, choose raw and pick JSON(application/json) from the dropdown. Try to insert one todo:

Postman to do nodejs api

If everything goes well, you will have a new todo on your mongo database.

A new POST todos endpoint

GET /todos Route

Next route is to fetch all the todos from our mongoDB database.

1. Modify server.js to add the GET route :

2. Open Postman and try to fetch the data using GET

GET-todos-route-nodejs

Getting Individual Resource using GET /todos Route

1. Modify server.js by Adding the following route:

2. Open Postman and try to fetch the data using GET

GET-individual-todo

DELETE a Resource using DELETE /todos/:id Route

1. Modify server.js by Adding the DELETE route:

2. Open Postman and try to delete a todo

delete-to-do-

Updating a Resource using PUT /todos/:id Route

1. Installing lodash

We will use a “pick” method from lodash, so we need to install it :

2. Update server.js by adding PUT route

3. Open Postman and try it out

put-to-do-update

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.