,

Implementing a RESTful CRUD API using Node, Express, MongoDB, and Postman (Part 3 of 3)

Posted by

Build Restful CRUD API with Node, Express MongoDB & Postman (Part 3 of 3)

Build Restful CRUD API with Node, Express MongoDB & Postman (Part 3 of 3)

By (Your Name)

Now that we have covered setting up our Node.js server and creating routes with Express, as well as integrating a MongoDB database in Part 1 and Part 2 of this tutorial series, it’s time to conclude by implementing the CRUD (Create, Read, Update, Delete) operations and testing them using Postman.

Step 1: Implementing CRUD operations in our API

First, let’s start by implementing the Create operation by adding a new POST route in our Express server. We will receive the data to be created in the request body and save it to our MongoDB database.


  const express = require('express');
  const app = express();
  const mongoose = require('mongoose');
  
  // Require our model
  const Product = require('./models/product');
  
  app.post('/products', (req, res) => {
    const { name, price } = req.body;
    
    const newProduct = new Product({
      name,
      price
    });
    
    newProduct.save()
      .then((product) => {
        res.json(product);
      })
      .catch((error) => {
        res.json({ message: error.message });
      });
  });
  

Next, we will implement the Read operation by adding a GET route to retrieve all products from our database and another GET route to retrieve a specific product by its ID.


  app.get('/products', (req, res) => {
    Product.find()
      .then((products) => {
        res.json(products);
      })
      .catch((error) => {
        res.json({ message: error.message });
      });
  });
  
  app.get('/products/:id', (req, res) => {
    const productId = req.params.id;
    
    Product.findById(productId)
      .then((product) => {
        res.json(product);
      })
      .catch((error) => {
        res.json({ message: error.message });
      });
  });
  

For the Update operation, we will add a PUT route to update a product by its ID. We will receive the updated data in the request body and use the findByIdAndUpdate method provided by Mongoose to update the product.


  app.put('/products/:id', (req, res) => {
    const productId = req.params.id;
    const { name, price } = req.body;
    
    Product.findByIdAndUpdate(productId, { name, price }, { new: true })
      .then((product) => {
        res.json(product);
      })
      .catch((error) => {
        res.json({ message: error.message });
      });
  });
  

Finally, we will implement the Delete operation by adding a DELETE route to remove a product from our database by its ID.


  app.delete('/products/:id', (req, res) => {
    const productId = req.params.id;
    
    Product.findByIdAndRemove(productId)
      .then(() => {
        res.json({ message: 'Product deleted successfully' });
      })
      .catch((error) => {
        res.json({ message: error.message });
      });
  });
  

Step 2: Testing the API using Postman

Now that we have implemented all CRUD operations in our API, we can test them using Postman. We can make various requests such as creating a new product, getting all products, getting a specific product by its ID, updating a product, and deleting a product.

By using Postman, we can easily send different types of requests (GET, POST, PUT, DELETE) to our API endpoints and verify the responses to ensure that our API is working as expected.

Conclusion

Congratulations! You have now successfully built a Restful CRUD API using Node.js, Express, and MongoDB. You have also learned how to test your API using Postman to ensure that it is functioning as intended. You can now use this knowledge to build more complex APIs and further enhance your skills as a backend developer.

0 0 votes
Article Rating
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@underdogff399
10 months ago

❤❤❤ big sis, could you make a video on how to improve the logic building or you can tell me some practices to follow. Whenever I build logic so I take hard path and when I understand it has a smart path too, so I felt so dumb, I just need a proper mindset. ❤ I will appreciate your suggestion.

@gulabchandgupta5486
10 months ago

Nice 👍

@wasimanwar76
10 months ago

Nice Explanation Mam