ACM-VIT / SDG-Backend

0 stars 0 forks source link

Making a route to get details of all businesses #2

Open mahendra785 opened 3 weeks ago

mahendra785 commented 3 weeks ago

Make a route which returns the details of all the businesses in the database, so that we can display it on the website.

D-Vspec commented 3 weeks ago

Implement Route to Get Details of All Businesses

Objective

Create a route that returns the details of all businesses from the database. This will allow you to display business information on the website.

Steps

1. Define the Route in the Backend

Express Route Handler Example:


const express = require('express');
const router = express.Router();
const Business = require('../models/businessModel');  // Assuming the schema is in `models/businessModel.js`

// GET route to fetch all businesses
router.get('/businesses', async (req, res) => {
    try {
        // 1. Fetch all businesses from the database
        const businesses = await Business.find();

        // 2. Return the businesses in the response
        res.status(200).json(businesses);
    } catch (error) {
        // 3. Handle any errors (e.g., database issues)
        res.status(500).json({ error: "Server error. Please try again later." });
    }
});

module.exports = router;