NodeJS Controller

// Import necessary modules and dependencies
const YourModel = require('../models/YourModel');

// Define controller functions
const yourController = {
  // Function to retrieve all data
  getAllData: async (req, res) => {
    try {
      // Retrieve data using YourModel
      const data = await YourModel.find();
      // Send retrieved data as a response
      res.status(200).json(data);
    } catch (err) {
      // Handle errors and send appropriate response
      res.status(500).json({ error: err.message });
    }
  },

  // Function to retrieve data by ID
  getDataById: async (req, res) => {
    const { id } = req.params;
    try {
      // Retrieve data using YourModel based on provided ID
      const data = await YourModel.findById(id);
      // Send retrieved data as a response
      if (data) {
        res.status(200).json(data);
      } else {
        res.status(404).json({ message: 'Data not found' });
      }
    } catch (err) {
      // Handle errors and send appropriate response
      res.status(500).json({ error: err.message });
    }
  },

  // Function to create new data
  createData: async (req, res) => {
    const newData = req.body;
    try {
      // Create new data using YourModel
      const data = await YourModel.create(newData);
      // Send created data as a response
      res.status(201).json(data);
    } catch (err) {
      // Handle errors and send appropriate response
      res.status(500).json({ error: err.message });
    }
  },

  // Function to update data by ID
  updateDataById: async (req, res) => {
    const { id } = req.params;
    const updatedData = req.body;
    try {
      // Update data using YourModel based on provided ID and new data
      const data = await YourModel.findByIdAndUpdate(id, updatedData, { new: true });
      // Send updated data as a response
      if (data) {
        res.status(200).json(data);
      } else {
        res.status(404).json({ message: 'Data not found' });
      }
    } catch (err) {
      // Handle errors and send appropriate response
      res.status(500).json({ error: err.message });
    }
  },

  // Function to delete data by ID
  deleteDataById: async (req, res) => {
    const { id } = req.params;
    try {
      // Delete data using YourModel based on provided ID
      const data = await YourModel.findByIdAndDelete(id);
      // Send success message as a response
      if (data) {
        res.status(200).json({ message: 'Data deleted successfully' });
      } else {
        res.status(404).json({ message: 'Data not found' });
      }
    } catch (err) {
      // Handle errors and send appropriate response
      res.status(500).json({ error: err.message });
    }
  }
};

module.exports = yourController;

This Node.js controller file demonstrates basic CRUD (Create, Read, Update, Delete) operations using a model called YourModel. It defines functions for handling HTTP requests:

  1. getAllData: Retrieves all data from the model.
  2. getDataById: Retrieves data by a specific ID.
  3. createData: Creates new data.
  4. updateDataById: Updates existing data based on an ID.
  5. deleteDataById: Deletes data by a specific ID.

Each function utilizes asynchronous operations to interact with the model (YourModel) and handles potential errors that might occur during these operations. The functions respond with appropriate status codes and messages based on the success or failure of the operations.