Restful CRUD API with Express, MongoDB and Mongoose

This is another nodejs and express.js tutorial that shows how to use Mongodb and Mongoose ORM to create CRUD operations.

The Mongodb is a popular no-SQL database that is open source.

Mongoose is an object-relational mapping (ORM) framework that includes useful ways for working with Mongodb collections.

I have already shared Node js Rest Api to Add, Edit and Delete Record from MySQL Using Express JS. I am assuming you have installed mongodb into your server.

Mongodb and Mongoose

MongoDB will be the database, and mongoose ORM will be used to perform actions on Mongodb models. I’ll make an HTTP REST call to get information about mongodb collections, add new records in mongodb, update existing records in mongodb, and destroy collections.

This node js tutorial will cover the following features:

  • How to create a database connection with Mongodb.
  • How to create a Model using Mongoose.
  • How to create a collection in Mongodb using Mongoose.
  • How to delete collection in Mongodb using Mongoose.
  • How to update Collection in Mongodb using Mongoose.
  • Creating CRUD operation REST API using Node, ExpressJS, MongoDB, and Mongoose.

Rest End Points

The Node js Rest API details are as follows:

RouteMethodTypePosted JSONDescription
/employeesGETJSONGet all employees data
/employees/{id}GETJSONGet a single employee data
/employeesPOSTJSON{"employee_name": "Rachel", "employee_age": "44", "employee_salary" : "4521"}Insert new employee record into database
/employeesPUTJSON{"employee_name": "Rachel", "employee_age": "56", "employee_salary" : "5421", "id":44}Update employee record into database
/employeesDELETEJSON{"id" : 9}Delete particular employee record from database

Node JS Rest API

Let’s create a folder ‘nodejs-restapi-mongoose’ in the nodejs workspace.This is our nodejs project name and will have all files.

  • package.json: This file will have all nodejs dependencies module for this example.
  • config/database.js: This file will be used for database connection parameters for Mongodb.
  • model/employee.js: This file will be used to create an employee schema and model.
  • main.js: This file will use to create nodejs application server and routes url.
  • node_modules : This folder will contain all nodejs packages.

install dependencies

We will create a package.json file into ‘nodejs-restapi-mongoose’ folder and add the below JSON in this file.

We’ll open a command prompt and navigate to the 'nodejs-restapi-mongoose-example' folder, where we’ll run the command below to install the required modules in the ‘node modules’ folder.

d:\nodejs-restapi-mongoose-example > npm install

Create Database Connection

We’ll put a database.js connection file in the config folder, then add the mongodb connection URL like this:

Create MongoDB Model

We’ll save the employee.js model to the model folder, then construct a schema and register the Employee model.

In the above code, we have created an instance of mongoose and schema. We have defined our employee table schema here.

Create server file

created a main project entry server.js file.We’ll add dependent modules to this file.Added below code into the server.js file.

Rest Api to fetch all records

We’ll use a GET Rest Request to get all of MongoDB’s employee records. To get data from the employee database, we’ll use a mongodb query and send JSON data to the client as a response object.

Employee.find is a method for retrieving all records from a database. The res.end() method uses the JSON.stringify() method to transmit data to the client as a json string.

Now, using a browser, go to http://localhost:8888/employee rest api address to receive a list of all employees in the Mongo database.

I’ll cover the rest of the functionality in the next tutorial.

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.