,

Using Express JS and Node JS to Retrieve Data from Mongo DB in Back-End Development

Posted by






Back-End Development: Fetching Data from Mongo DB using Express JS and Node JS

Back-End Development: Fetching Data from Mongo DB using Express JS and Node JS

Back-end development involves working with server-side programming languages and databases to create the functionality of a web application. In this article, we will focus on fetching data from a MongoDB database using Express JS and Node JS.

Setting Up the Environment

Before we can start fetching data from a MongoDB database, we need to set up our development environment. Make sure you have Node JS and npm installed on your machine. You will also need to install MongoDB and have it running locally or on a remote server.

Initializing the Project

Once the environment is set up, create a new directory for your project and navigate to it in your terminal. Use the following commands to initialize a new Node JS project, and install the necessary dependencies:


$ mkdir fetch-data-from-mongodb
$ cd fetch-data-from-mongodb
$ npm init -y
$ npm install express mongoose

Creating a Server with Express JS

Express JS is a minimal and flexible Node JS web application framework that provides a robust set of features to develop web and mobile applications. We will use Express JS to create a server and handle HTTP requests.

Creating a file called app.js and adding the following code:


const express = require('express');
const app = express();

app.get('/', (req, res) => {
// Code to fetch data from MongoDB
});

const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});

Connecting to MongoDB with Mongoose

Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment. It provides a straight-forward, schema-based solution to model your application data. We will use Mongoose to connect to our MongoDB database and fetch data.

Update app.js to include the following code:


const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/mydatabase', { useNewUrlParser: true, useUnifiedTopology: true });

const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
console.log('Connected to MongoDB');
});

Fetching Data from MongoDB

With the MongoDB connection set up, we can now fetch data from the database. We can use Mongoose models to define the structure of our data and query the database to retrieve it.

Adding the following code inside the app.get callback in app.js:


const MyModel = mongoose.model('MyModel', new mongoose.Schema({
name: String
}));

app.get('/', (req, res) => {
MyModel.find({}, (err, data) => {
if (err) {
res.status(500).send(err);
} else {
res.status(200).send(data);
}
});
});

Running the Server

To run the server, execute the app.js file using the following command:


$ node app.js

Now, you can visit http://localhost:3000 in your web browser to see the data fetched from your MongoDB database.

Conclusion

In this article, we have learned how to fetch data from a MongoDB database using Express JS and Node JS. We set up a server with Express JS, connected to our MongoDB database using Mongoose, and fetched data from the database. Back-end development is a crucial part of web application development, and being able to fetch data from a database is an essential skill for any back-end developer.