Most of the applications you use these days follow the Client-Server Architecture. The Application itself is considered the Client or the Front-End part, and it needs to talk to a Server or the Back-End to load or pull the data. REST is basically a convention for building the HTTP services used to establish this communication. You can easily implement the REST API approach by using Express JS as the Back-End Server and MongoDB as the Document Store. This article will help you create REST APIs with Node Express MongoDB.
MongoDB is a highly popular and widely used open-source Database, used by developers all over the world. Express is one of the most popular Web Frameworks for Node.js, and it supports Routing, Middleware, and View Systems. MongoDB helps developers build applications and websites without spending much time configuring a Database. Before building REST APIs using Node Express MongoDB, let’s discuss this robust Database platform in brief.
What is MongoDB?
MongoDB is an open-source, cross-platform, and Non-Relational Database Management System. Developed in 2009 by MongoDB Inc, MongoDB uses the document-oriented Database Model to group data into documents and collections rather than tables. This allows it to store different types of data. It supports multiple languages, comprises excellent features, and provides high performance.
MongoDB is a popular NoSQL Database that supports a flexible schema approach and is capable of storing large data sets efficiently. It is written in different programming languages like JavaScript, Python, Java, PHP, C++, C, Ruby, and Perl. On top of that, it is released under the Server Side Public License (SSPL) which supports a unique mechanism to store and retrieve high-volume data. MongoDB is a simple and easy to configure program but offers high performance, automatic scalability, and high availability.
Key Features of MongoDB
Below are some of the key features of MongoDB that can be attributed to its growing popularity.
- Schema-less: MongoDB is a Non-Relational Database that stores and uses documents and collections to retrieve data. With MongoDB, you are not required to create a table or pre-defined schema.
- Scalability: Unlike other SQL Databases, MongoDB uses horizontal scalability which allows users to generate clusters with real-time replication. MongoDB supports the Sharding process, which makes it easy to horizontally scale the data across multiple Servers.
- Cost-Effective: MongoDB is a free, open-source Non-Relational Database Management program that allows you to store large data sets in a cost-effective manner.
- Code-native Data Access: MongoDB stores all the collected data in a JSON-like document which allows changes/modifications over time. This makes it easy for developers to retrieve information in the data structure using any programming language.
- High-Performance: MongoDB is fast and easy to use because of its NoSQL nature. Data can be stored, manipulated, and retrieved quickly without compromising Data Integrity.
Hevo Data is a No-code Data Pipeline that offers a fully managed solution to set up data integration from MongoDB and 150+ Data Sources (including 40+ Free Data Sources)and will let you directly load data to a Data Warehouse or the destination of your choice. It will automate your data flow in minutes without writing any line of code. Its fault-tolerant architecture makes sure that your data is secure and consistent. Hevo provides you with a truly efficient and fully automated solution to manage data in real-time and always have analysis-ready data.
Get started with hevo for free
What are Node and Express?
Node.js or Node is an open-source, cross-platform runtime environment that runs on the V8 engine and allows developers to build Server-side Tools, Programs, and Applications in JavaScript. It is used for executing JavaScript code outside of a browser. Node is used to build highly scalable, data-intensive, and real-time Back-End Services also called APIs (Application Programming Interfaces) to power the Client Applications.
Node is very easy to get started with and it can be used for prototyping and agile development. On top of that, it can also be used for building super fast and highly scalable services. It’s used in production by large companies such as PayPal, Uber, Netflix, Walmart, and so on.
Express.js or Express is a very popular Back-End Web Framework for Node.js. Express is built upon Node and it just offers a variety of tools that make working with Nodes much easier and fun. It implements a lot of functionalities that help us with Routing, Middleware, and View Systems. Released as free and open-source software under the MIT License, Express is designed for building web applications and APIs.
What is REST API?
An Application Programming Interface (API) establishes a connection between computers or between computer programs (applications) by providing readily available codes and information pipelines. It is a type of software interface that acts as a mediator among other pieces of software to streamline the interaction with one other. Owing to diverse application architectures, different types of APIs (such as Program, Local, Web, or REST API) assist developers in building robust digital solutions.
REST APIs provide a flexible, lightweight way of integrating computer applications. REST APIs are a simple and standardized approach to communication, which means you don’t have to worry about how to format your data, it’s all standardized and industry use. REST APIs are scalable as well, which means as your service scales, you don’t have to worry about the growing complexity. You can easily make modifications to your data and keep track of that across Clients and Servers. They support caching which ensures high performance in large part.
Key Benefits of REST API
1) Flexible
REST API is flexible with multiple types of calls like returning different data formats and changing structurally with the correct implementation of hypermedia. It allows users to communicate back and forth with clients and servers, even if they are hosted on different servers.
2) Adaptable
REST API is adaptable to any modification done in data that resides in the database, even when hosted on the different back- and front-end servers. Since it depends to a certain extent on codes, it helps synchronize data within websites without any issue.
3) Ease of Understanding
As REST uses HTTP verbs (GET, POST, PUT or DELETE) methods for communication, these methods are self-explanatory. In addition, REST architecture helps increase developers’ productivity, allowing them to display the information on the client-side and store or manipulate the data on the server-side.
Node Express MongoDB System Architecture
Now that you’re familiar with Node Express MongoDB, let’s dive into the main part of this article. This article will help you to use Express with MongoDB Atlas, MongoDB’s Database-as-a-Service platform, to expose RESTful API endpoints for your Client Applications.
Using Express, you can create a Back-End Middleware for running Node.js Server and it will expose the REST API routes to the application you’re creating. The Express.js Server can also connect to the MongoDB Atlas Cluster via the Node.js Driver.
The Front-End Application will be written in React to use the REST API endpoints hosted on the Express.js Server. As you can see, this is how Node Express MongoDB will come into play while building the REST API.
In this demonstration, you’ll be creating a Tinder-like application for the sample_airbnb Database which contains information on various listings. You can load this sample dataset into the Atlas cluster.
Just like Tinder, users can swipe the listing cards on this app to save or drop them and press the “like” button to add likes. On top of that, one can double click on the cards to know complete details about the cards.
So, let’s get started with Node Express MongoDB to create this application.
Steps to Build a REST API using Node Express MongoDB
Before getting started with Node Express MongoDB, you will need to deploy an Atlas Cluster. You can refer to the official Getting Started with Atlas guide to creating a free Atlas Account. You can then go on to create your first cluster and get your connection string to the Database.
Now, you can simply load the sample dataset by clicking on “Load Sample Data.” You now need to clone the project stub branch. You can explore the full project in the following GitHub repo.
git clone -b stub
git@github.com:mongodb-developer/mongodb-express-rest-api-example.git
In the project, go to the “server” directory to install the needed packages.
cd mongodb-express-rest-api-example/server
npm install
Follow the below-mentioned steps to build the REST API using Node Express MongoDB.
Connecting to MongoDB Atlas
You are now ready to connect the Express Server to the MongoDB Atlas Cluster. After locating your connection string, you need to create a config.env file in the Server Directory. You can now assign the value of the connection string to a new ATLAS_URI variable. Don’t forget to replace the <username> and the <password> with the username and password of your Database.
ATLAS_URI=mongodb+srv://<username>:<password>@sandbox.jadwj.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
server/db/conn.js allows Atlas Database to be accessed globally by exporting a MongoDB Client that can be used by any other module. Open server/db/conn.js and add the implementation of the connectToServer function as shown.
const { MongoClient } = require("mongodb");
const connectionString = process.env.ATLAS_URI;
const client = new MongoClient(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
let dbConnection;
module.exports = {
connectToServer: function (callback) {
client.connect(function (err, db) {
if (err || !db) {
return callback(err);
}
dbConnection = db.db("sample_airbnb");
console.log("Successfully connected to MongoDB.");
return callback();
});
},
getDb: function () {
return dbConnection;
},
};
This module exports the _db variable, which will hold the “sample_airbnb” Database-level object. You’re now done with the first step of building a REST API using Node Express MongoDB.
Adding REST API CRUD Routes
Express.js exposes REST API routes to your application to perform Create, Read, Update, and Delete (CRUD) operations. The file that will host the routes is “server/routes/record.js”. It uses the Express Router feature.
const express = require("express");
// recordRoutes is an instance of the express router.
// We use it to define our routes.
// The router will be added as a middleware and will take control of requests starting with path /listings.
const recordRoutes = express.Router();
Read Route
Whenever the /listings path on a GET method is called, the Read route will be used. It uses a collection.find() method to query the listingAndReviews collection for the first 50 available listings.
// This section will help you get a list of all the documents.
recordRoutes.route("/listings").get(async function (req, res) {
const dbConnect = dbo.getDb();
dbConnect
.collection("listingsAndReviews")
.find({}).limit(50)
.toArray(function (err, result) {
if (err) {
res.status(400).send("Error fetching listings!");
} else {
res.json(result);
}
});
});
The code sends back the output as the API response.
Create Route
The Create route documents a “match” swipe in a “matches” collection. The body of this POST method will have a user session_id, the swiped direction, and the listing_id in order to create a “match” document.
// This section will help you create a new document.
recordRoutes.route("/listings/recordSwipe").post(function (req, res) {
const dbConnect = dbo.getDb();
const matchDocument = {
listing_id: req.body.id,
last_modified: new Date(),
session_id: req.body.session_id,
direction: req.body.direction
};
dbConnect
.collection("matches")
.insertOne(matchDocument, function (err, result) {
if (err) {
res.status(400).send("Error inserting matches!");
} else {
console.log(`Added a new match with id ${result.insertedId}`);
res.status(204).send();
}
});
});
The collection.insertOne() method saves the collection with the prebuilt “matchDocument.”
Update Route
The Update route updates the “likes” tab on a listing object. This is also done via a POST method.
// This section will help you update a document by id.
recordRoutes.route("/listings/updateLike").post(function (req, res) {
const dbConnect = dbo.getDb();
const listingQuery = { _id: req.body.id };
const updates = {
$inc: {
likes: 1
}
};
dbConnect
.collection("listingsAndReviews")
.updateOne(listingQuery, updates, function (err, _result) {
if (err) {
res.status(400).send(`Error updating likes on listing with id ${listingQuery.id}!`);
} else {
console.log("1 document updated");
}
});
});
The collection.updateOne() method with a $inc on the “like” field is used to increment the likes.
Delete Route
A listing can be deleted from the Database after a listing is dropped. This can be done via the Delete route.
// This section will help you delete a record.
recordRoutes.route("/listings/delete/:id").delete((req, res) => {
const dbConnect = dbo.getDb();
const listingQuery = { listing_id: req.body.id };
dbConnect
.collection("listingsAndReviews")
.deleteOne(listingQuery, function (err, _result) {
if (err) {
res.status(400).send(`Error deleting listing with id ${listingQuery.listing_id}!`);
} else {
console.log("1 document deleted");
}
});
});
The :id parameter specifies Id of the listing to be deleted via collection.deleteOne().
You can now go ahead and launch the Server.
npm start
[nodemon] starting `node server.js`
Successfully connected to MongoDB.
Server is running on port: 5000
Setting Up the Front-End
This React application mainly consists of the App.js React file and class.
import './App.css';
import TinderCard from 'react-tinder-card'
import axios from 'axios';
import { Component } from 'react';
import { v4 as uuid } from 'uuid';
class App extends Component {
constructor() {
super();
this.state = {
data: [],
session_id: uuid(),
liked: false
};
this.handleClick = this.handleClick.bind(this);
this.showDetails = this.showDetails.bind(this);
}
async onSwipe(direction, listingId, sessionId) {
this.setState({
liked: false
});
if (direction === "left") {
await axios.delete(`http://localhost:5000/listings/delete/${listingId}`)
} else {
await axios.post("http://localhost:5000/listings/recordSwipe", { id: listingId, session_id: sessionId, direction })
}
}
async handleClick(listingId) {
this.setState({
liked: !this.state.liked
});
await axios.post("http://localhost:5000/listings/updateLike", { id: listingId });
}
showDetails(listing) {
alert(`Name: ${listing.name}n Price : $${listing.price['$numberDecimal']} n Minimum Nights : ${listing.minimum_nights}n Beds : ${listing.beds}`);
}
async componentWillMount() {
const response = await axios.get(`http://localhost:5000/listings`);
const json = await response.data;
this.setState({ data: json });
}
render() {
const likeButtonLabel = this.state.liked ? '❤' : 'Like';
return (
<div className="app">
<div>
<h1>LisTinder</h1>
<h2>Swipe left for drop or right to save...</h2>
<div className="card-container">
{this.state.data.map((listing) =>
<TinderCard className='swipe' key={listing.name} onSwipe={(dir) => this.onSwipe(dir, listing._id)} >
<div style={{ backgroundImage: 'url(' + listing.images.picture_url + ')' }} className='card'>
<div className="card-details">
<h3>{listing.name}</h3>
<div className="card-actions">
<button className="button" onClick={() => this.handleClick(listing._id)}>{likeButtonLabel}</button>
<button className="button" onClick={() => this.showDetails(listing)}>See Details</button>
</div>
</div>
</div>
</TinderCard>
)}
</div>
</div>
</div>
);
}
}
export default App;
You can use some third-party modules to enhance your Front-End by creating the swiping tiles and graphics.
Now, let’s start the application you just created using Node Express MongoDB in a new terminal.
cd ../app/listings
npm install
npm start
Testing the Application
Now that all the components of the app are up and running, you can open the http://localhost:3000 URL to test your app.
That’s it your REST Application using Node Express MongoDB is finally ready.
Conclusion
APIs are very common nowadays, and they are used everywhere on a website. Using Express to create a Back-End Middleware for running Node.js Server to create a REST API is a popular MongoDB stack design. This article helped you create a REST API using Node Express MongoDB that supports CRUD operations.
MongoDB is a great tool for storing and managing your project data. Nowadays, many more companies want to associate themselves with MongoDB. To get a complete overview of your business performance, it is important to consolidate data from MongoDB and other Data Sources into a Cloud Data Warehouse or a destination of your choice for further Business Analytics. This is where Hevo comes in.
visit our website to explore hevo
Hevo Data with its strong integration with 150+ Data Sources & BI tools such as MongoDB, allows you to not only export data from sources & load data in the destinations, but also transform & enrich your data, & make it analysis-ready so that you can focus only on your key business needs and perform insightful analysis using BI tools.
Give Hevo Data a try and sign up for a 14-day free trial today. Hevo plans & pricing for different use cases and business needs, check them out!
Share your experience of working with Node Express MongoDB in the comments section below.
Raj, a data analyst with a knack for storytelling, empowers businesses with actionable insights. His experience, from Research Analyst at Hevo to Senior Executive at Disney+ Hotstar, translates complex marketing data into strategies that drive growth. Raj's Master's degree in Design Engineering fuels his problem-solving approach to data analysis.