Create CRUD Application in Express JS

Now in this medium story, we can create a simple APIs in Express JS ( Node JS Framework ). In this story, we can create a CRUD (Create, Read, Update, Delete) APIs. If you are new in the Node JS and want to learn how to create the basic server and create a basic APIs in Node JS, please read my medium story: Create HTTP Server in Node JS

Create a project in Express :

If you are new to Express framework and doesn’t know how to create a project in Express.js, please refer my medium story: Setup Basic Server with Express Framework. For creating a project we run the below command in the command prompt where you want to create the project.

Project Structure:

Create properties.js file :

In the properties.js file, we define the port number in which the server is listening and define the URL of MongoDB, it is used for connecting backend code with the database.

Create database connectivity file :

This file holds the code of database connectivity. If you are new to MongoDB and mongoose, so refer to my medium story: Best Practice of Mongoose connection with MongoDB.

Step 1 : In the first step, we can require the mongoose, chalk and MongoDB URL from config/properties.js. Mongoose is ODM(Object Document Model) for Node.js and we require to chalk for giving colors to text on console and properties.js for getting DB URL.

Step 2 : After that, we declare the chalk variable depends upon various connections like connected, disconnected, terminated etc.

Step 3 : In this step, we create a connection function. In this function, we can use mongoose.connect( ) function for connecting the MongoDB database and handle all the events like connected, error, disconnected and terminated.

Create a server.js file :

After that, we can start a server by below command :

There are lots of advantages of nodemon over node, please refer my medium story: Setup Basic Server with Express Framework

Creating Heros Model :

In this file, we create a schema of Heros by the use of mongoose.schema. The schema is the organisation of data as a blueprint of how the database is constructed. In this schema, we create a blueprint of Hero which having name and description.

Step 1 : In this step, we require the mongoose package.

Step 2 : Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.

Step 3 : After that, we define the Schema( structure of document/data) in MongoDB by the use of mongoose.Schema.

Step 4 : In the last step, we export the schema and later importing in dao file.

Create Data Access Object file :

In the Data Access Object (DOA) layer, we can define the function which is directly connected to the database and fetch data and save data from and to the database.

Step 1 : In this step, we require the mongoose package and hero.model.js file.

Step 2 : In the second step, we define all the function by the use of mongoose.statics. Statics are pretty much the same as methods but allow for defining functions that exist directly on your Model.

Step 3 : In this step, we can register the Schema with mongoose.model.

Step 4 : In the last step, we export the mongoose.model and later importing in the controller file.

Create controller file :

In this file, we put all the business logic. In this file, we create all the function like create a hero, get a list of hero, get a single hero according to name, update the hero, delete the hero.

Give routes to Heros api with route.js file :

In this file, we create routes of rest api.

Update server.js file :

This is the updated server.js file. In this file, we added the routes of REST APIs, active body-parser and handle the errors. In this file, I added the self-explanatory comments.

API Calls :

Below are the all screenshots of all APIs call, which having REST APIs and result of these APIs.

Create Hero:

Get All Heros:

Get Hero :

Update a Hero:

Delete a Hero :

Conclusion:

This is the explanatory medium story of how to create REST APIs in Express.js with the help of MongoDB. If you have any doubts, please mail me on vsvaibhav2016@gmail.com.

Software Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store