Stepping stone to Web Application Development with Express JS, Mongo DB

If you are planning to develop your high-quality software, and for that, you are wondering how to start with the APIs and set up a backend, then you are at the right place. Get ready to launch your application with the help of this 20 minute easy read.

Prerequisite

  1. Java Script
  2. Node JS
  3. Database

(Prior knowledge of Express JS, Mongo DB will be helpful)

Goal

  1. Express JS
  2. Mongo DB

Now let’s get started with the setup (suit up 😉)

Set-up

npm init

This will create your package.json. And now run -

npm install express mongoose --save

This will add Express JS and Mongoose, then save them in the dependency list. For more read on what is dependency list, you may visit

https://www.geeksforgeeks.org/what-is-the-meaning-of-save-for-npm-install/

Mongoose works as ODM(Object Data Mapping), helps in communication between JS and Mongo DB Model (those who are not familiar with the terms of No-SQL, consider it as an ORM — Object Relation Mapping)

Now create a file app.js (any name you can choose). In app.js write the below code -

and then in this current location where app.js, package.json resides, run -

node app.js

Now go to your browser and hit localhost:8081/ and you will see -

So congratulations ✌️ You have successfully run your Web Application. Now it’s time to add flavors of code to it.

First, we will create a Database in Mongo, where we will create a collection of USER (those who are not familiar with the terms of No-SQL, consider a collection as a table). We can install Mongo DB in our local machine and serve as a DB Server. But for the time being, we will create Mongo DB in the cloud using Mongo Atlas. Find the cloud link at — https://cloud.mongodb.com/. Create your free account and create a cluster where you have to create database and collection. Please follow the easy steps mentioned at —

https://docs.atlas.mongodb.com/tutorial/create-new-cluster/

Code Begins

Now create user.adapter.js in /MYORG/user. This file contains the methods that communicate with the collection and do all the necessary operations.

Now create a folder Repository and put index.js in MYORG/Repository. This file will include all the repositories such as user through which we can call the methods of adapter. It will also contain the code to connect with Mongo DB Server. You will get your connection string from your Mongo DB Atlas by clicking on CONNECT option and select “for the application” option

Goto your Mongo Atlas dashboard and click accordingly
Then select ‘application’ and the Driver as Node. Remember to replace username, password, database name accordingly. Visit Mongo Atlas Doc for more help

The code of index.js looks like -

That after create the service folder and create user.service.js in MYORG/service. This file will help the API to communicate with adapters, as well as one can add custom logics, validations etc in this layer.

Now modify the app.js where we will add API entry points which operates various operations on User model.

So finally the folder structure looks like something -

Time to Launch

Open your command prompt in MYORG as earlier you ran to start the application and run the same command like mentioned in a above section -

node app.js

If everything is perfect, you will see the message “App listening at http ://localhost:8081

Testing

  1. listUser (GET) - Lists all the users from collection
  2. userById (GET) - Fetch the user information by ID
  3. userByEmail (GET) - Fetch the user information by email
  4. addUser (POST) - Create a new user in collection
  5. updateuser (PUT) - Update a user details in collection by ID
  6. removeUser (DELETE) - Delete a user from the collection by ID

I have tried to show various sort of http methods, also how to use request parameters in body, query and path. For more operations, you may follow the docs of Monggose, you can play with the collection using any of the Queries from Mongoose — https://mongoosejs.com/docs/

So in postman you run -

Few examples of the APIs. You change values of host, port and other request details accordingly

Bingoo…. 💃 So you are ready to build your own middleware and create APIs that you can consume in your beautiful applications

Find the code in — https://github.com/gitsumanmandal/expressmongoappscratch/tree/master

Software Developer and Consultant | Tech & Travel Blogger