Creating API Endpoints Using ExpressJS | MERN Stack Part 3

Creating API Endpoints Using ExpressJS | MERN Stack Part 3

Express is one of the frameworks used in the MERN stack, In this article, let's see how to get started with ExpressJs by creating API endpoints and later extending it to its full potential by integrating with other MERN technologies.


As we need to set up the Environment and make the Endpoints this article is divided into 2 parts, in this article we shall be setting up the environment to work with. If you don't know much about the MERN Stack, and MongoDB please go through the below articles:-



Getting Started with ExpressJS:-

                As the Tag line tells, Express is a Fast, unopinionated, and minimalistic web framework for Node.js. It is fully written in Javascript, It is an open-source technology maintained by Node.js. Let's understand the meaning of each of the taglines, First, as we are directly building API endpoints without any middleware the robustness of the website increases, Second the most popular Frameworks like Django and other web development frameworks are opinionated which means to make a web application you need to follow a procedure or methodology and in that way, only you can develop it.

                Even Being a Framework Express allows users to develop applications in their own way, Third Minimalist because its really simple understanding how things work in Express as there is no back or hidden processes running, everything is done by the developer. Let's see how easy is to make web servers using Express by making our own first "hello world !" application using it.

Setting up Environment:-

                    For this, you can work in any IDE like visual studio code, visual studio, or sublime text (I recommend you to use IDE and not text editors). We will be using Visual studio code as most of the libraries and tools are already available there, and we can access the terminal inside the IDE.

1. Installing Node.js to use EpressJS, 

If you haven't installed Node.Js there are many videos on how to install go through them and install Node.JS or you can directly install from the given website:-

Stable Version: Node.js | Downloads 

It's recommended to use a stable version rather than the latest version as most libraries work fine in the stable version, you can verify the download by using the following command


We can see that I have checked another module called NPM which comes with a node.js, NPM is a package manager by which we can install libraries of javascript with simple commands. It's very useful to develop web projects with Javascript and Node.js

2. Initializing the Environment using NPM

You can start the initialization of the web application project using the command " npm init " in the required path by using the terminal, it will ask you come specification you can leave the default values and proceed
.

Now you can see that there is a new file called package.json file which contains the details of the project you working on. This file is actually one of the important files in the application.

What is Package.json File?

                If you developed any application you would know that sometimes we might need some libraries in order the working, if someone else wants to run the application they also need to install the libraries, Package.json simplifies this problem. Let's understand this with a simple example.

                Considering we are developing a Node.js project then we might use many libraries or dependencies, we can install them using NPM now while installing the dependencies npm will add their information of them in the package.json file along with their version no. Now suppose we want to upload our project to GitHub or someone wants to use our application they can simply take the main code or files of the project without the node_modules folder (Node_modules is the folder in which all the libraries and dependencies are installed by the package manager). The other person can simply run a command called "npm install" from which all the packages listed in the package.json file will be downloaded, so that's why the package.json file is important in nodeJs projects.

3. Installing required Dependencies:-

For this demonstration we will be using Express and Nodemon, Nodemon is a web server that is used by developers it reloads the servers when there is a change in the files or the files are saved again.
You can download the dependencies using the npm install command.


You can see that, I have used a tag -D while installing nodemon because it is developer dependency and not used in the production it is only used for testing the web application.

4. Creating an Entry Point

While using the npm init we have also agreed of creating an entry point as a file called index.js. Index.js file is the main file from which the web server starts working it's like the root file. Here we can use the express modules to create API endpoints and routes etc. After all the above operations the file structure should be like this.


In the Next Article, we shall be using the Expressjs Framework by creating a Helloworld program using routs and API calls.

Post a Comment

Previous Post Next Post