DEV Community

Cover image for Building RESTful APIs using MERN
Alim Mohammad
Alim Mohammad

Posted on

Building RESTful APIs using MERN

Building RESTful APIs using MERN

In this article, we will learn how to build RESTful APIs with MERN as API endpoints which is crucial to the functioning of a full-stack application. We will be making them from scratch with an explanation.

MERN is a popular web development stack used to build websites and webpages comprising services such as Database, Backend and Frontend. The main programming language used in this tech stack is Javascript, which helps in creating applications that are dynamic in nature.


How to build RESTful APIs in MERN stack

Firstly, let us get started with building APIs by installing all the required needs and dependencies that must be present on our system. In our next step, we have to install the basic dependencies like NodeJS and MongoDB in case they are not already installed in our system else we are good to go. Now that we are done installing dependencies, we must install the popular libraries of express and mongoose using the node package manager. The syntax of both can be defined as we install them each using our terminal.

npm install express
npm install mongoose
Enter fullscreen mode Exit fullscreen mode

Now that the installation is done, we have to let the system know that we want to start creating our project and to fulfill that we write the npm init command to start with our project after which a package.json can be visible in the project folder consisting of all the relevant information about the project.

2. Build the RESTful API

To create a REST API for our web application, ExpressJS framework is used. The steps to build are:-

  • Once we have installed the Express library using the command, npm install -g express-generator globally that will let you create your express project anywhere from our system without any hassle.
  • Open the terminal and enter the given command: express myproject and in case the ‘myproject’ looks odd here with the command understanding, it is important to know that ‘myproject’ can be the desired user-provided name for creating an express project.
  • In order to install the necessary dependencies, run the npm installcommand to initiate the project. It is required if you are using any added packages such as Mongoose.
  • API is defined in this step that indicates how well the data is transferred or received across two or more connected endpoints. Another important feature to look up to is the router object, created for defining the API endpoints.
  • A method such as GET, POST, PUT, and USE sets up the middleware to function routes at the application level.

Example code for creating a RESTful API:-

const express =   require('express');
const router = express.Router();
router.get('/users', (req, res) => {
request for endpoint /users });

router.post('/users', (req, res) => {
request for endpoint /users });
module.exports = router;
Enter fullscreen mode Exit fullscreen mode

As we discussed earlier Mongoose that it is a library that can be used for data models and schema but like most of the applications does not entirely need this, it completely depends upon the requirement of the user or project manager to include it. When it comes to the need of using MongoDB for our web application, we must create a connection by writing the following command.

mongoose.connect('mongodb://localhost/mydatabase')
Enter fullscreen mode Exit fullscreen mode

In the above code snippet, you can be confused about what ‘mydatabase’ means, so it can be assumed as your choice of name for the project.


Authentication and Authorization of the Web Application

Now that we are done creating the web application, we must ensure the security and integrity of the project. You need to follow certain steps to ensure the security of the project

Implementation of JWT in the respective web application is a way to secure your API and it generates a token that can be put into use to make your API secure. They must be used along with other reliable techniques like cookies, and cross-site request forgery tokens to avoid any threats.

OAuth is a secure way but relies on third-party services like Google, Apple and Github for your authentication. Performing this technique assigns a token that can be used to access the API.


Conclusion

In this article, we saw how we can set up the dependencies and write RESTful APIs that can be used for the passage of data. We further learnt how to create a REST API in the MERN stack by following an ordered sequence of installing dependencies, creating API endpoints, and setting up authentication using JWT and OAuth.


Top comments (0)