Creating a CRUD REST API/Service with Nodejs , Express and Mongodb

By

This guide will help you create a CRUD REST API/Service with Nodejs , Express and Mongodb . We will create a mongo Student model and expose it using Student Resource.

What You will learn

  • Defining a RESTful Service?
  • Fundamentals of designing a REST API.
  • How to create a RESTful Service with all CRUD operations?
  • How to use Nodejs Express and MongoDB to create a RESTful API?
  • How to run various REST API with Postman?
  • What are the differences between GET, POST, PUT and DELETE methods in a request ?

Overview of Express Mongodb Rest CRUD API example

We will build a simple Nodejs Express CRUD REST API for a Todo application in that:

  • Each Todo has id, title, description .
  • Api’s help to create, retrieve, update, delete Todo.
  • Apis also support custom finder methods such as find by title or by id.

These are APIs that we need to create :

MethodsUrlDescription
GET/bookRetrieve all Book Lists
POST/bookAdd a new Book
PUT/bookUpdating an existing Book
GET/book:idGet details of specific Book
DELETE/book:idDelete a specific Book

Project Structure

Have a look at the Project Structure we are going to use in this post and also going forward.

nodejs project structure

Models

Models will contain your backend MongoDB models and database schemas . We are going to use the models under this directory in our routes.

Routes

Routes are basically the API endpoints for a specific domain object . We are using domain based approach here . All the API related to user or book domain will reside here .

Lib

As the name suggests all the lib wil reside in lib folder , any common logic that you want to use should reside in the lib fodler.

Controller

Controller will have the logic for a routing and will be used as a callback inside routes.

Lets get started and have a look how we can write a basic CRUD rest api.

Configure Environment Variables

Before creating and coding any application we are required to have our environment variable setup in place . Lets go ahead and create a .env file in my working directory . Add the required configurations in your environment file . For now we will add PORT and MongoURL .

PORT=9000
MONGO_URI=mongodb://localhost:27017/crud-node

Configure and Connect with MongoDB

In order to connect with mongodb we need to create a file as shown above under config directory. Have a look at how we can Dockerize Nodejs Application .

mkdir config
vi mongo.js

Now we have create a file then we need to import the mongoose , first install mongoose like below.

npm install --save mongoose 

Now we need to import mongoose the file we have created in our server.js . Add the below line of code which will connect with MongoDB.

Note:- Make sure you have mongod service running and your mongodb is intstalled correctly.

Defining MongoDB Schema/Models

Lets go ahead and now define our mongo schemas for our CRUD application . We are going to create a users schema .

Learn More From Our Series