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.
Few minimal knowledge which is required to understand the development described below are -
- Java Script
- Node JS
(Prior knowledge of Express JS, Mongo DB will be helpful)
We will build a backend and middleware to create, read, update, delete users and expose the API endpoints, with the followings -
- Express JS
- Mongo DB
Now let’s get started with the setup (suit up 😉)
First, you have to download NodeJS from https://nodejs.org/en/ and install it in your system. Then choose one of your favorite editors (VS Code in my case). Now create a workspace say MYORG. Goto MYORG and run from the terminal -
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
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 -
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 —
Create a folder user under /MYORG and inside that create a file user.model.js (You can skip to ending to see the file structure). This file contains the model of the user, you can add whatever you want to store in user details.
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
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 -
If everything is perfect, you will see the message “App listening at http ://localhost:8081”
Dowonload any REST Client (I am using Postman. Download it from — https://www.postman.com/downloads/). Now run the APIs accordingly. I have created 6 APIs as follows -
- listUser (GET) - Lists all the users from collection
- userById (GET) - Fetch the user information by ID
- userByEmail (GET) - Fetch the user information by email
- addUser (POST) - Create a new user in collection
- updateuser (PUT) - Update a user details in collection by ID
- 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 -
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