Steps to Create Server

Step 1: Create a new directory named backend.

mkdir backend
cd backend

Step 2: Create a server using the following command in your terminal.

npm init -y

Step 3: Install the necessary package in your server using the following command.

npm install express mongoose cors

Dependencies:

 "dependencies": {
"cors": "^2.8.5",
"express": "^4.19.2",
"mongoose": "^8.3.2"
}

Folder Structure:

project structure

Step 5: Create a file ‘index.js’ and set up the server.

JavaScript
//index.js

const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");

const app = express();
const PORT = process.env.PORT || 5000;
app.use(cors());

// Connect to MongoDB
mongoose.connect("<MONGODB_URL>", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});

// Define a Donation schema
const donationSchema = new mongoose.Schema({
    name: { type: String, required: true },
    amount: { type: Number, required: true },
    description: { type: String, required: true },
});

const Donation = mongoose.model("Donation", donationSchema);

// Define a Total Amount schema
const totalAmountSchema = new mongoose.Schema({
    total: { type: Number, required: true },
    current: { type: Number, required: true },
});

const TotalAmount = mongoose.model("TotalAmount", totalAmountSchema);

// GET route to fetch total amount
app.get("/api/totalAmount", async (req, res) => {
    try {
        // Fetch total amount
        const totalAmount = await TotalAmount.findOne();
        res.json(totalAmount);
    } catch (error) {
        console.error(error);
        res.status(500).send("Internal Server Error");
    }
});

// PUT route to update current amount
app.put("/api/totalAmount", async (req, res) => {
    try {
        const { current } = req.body;

        // Update current amount
        const totalAmount = await TotalAmount.findOne();
        totalAmount.current = current;
        await totalAmount.save();

        res.status(200).send("Current amount updated successfully");
    } catch (error) {
        console.error(error);
        res.status(500).send("Internal Server Error");
    }
});

// API routes for donations
app.post("/api/donations", async (req, res) => {
    try {
        const { name, amount, description } = req.body;

        // Create a new donation
        const newDonation = new Donation({ name, amount, description });
        await newDonation.save();

        // Update the current total amount
        const totalAmount = await TotalAmount.findOne();
        totalAmount.current += amount;
        await totalAmount.save();

        res.status(201).json(newDonation);
    } catch (error) {
        console.error(error);
        res.status(500).send("Internal Server Error");
    }
});

app.get("/api/donations", async (req, res) => {
    try {
        // Fetch all donations
        const donations = await Donation.find();
        res.json(donations);
    } catch (error) {
        console.error(error);
        res.status(500).send("Internal Server Error");
    }
});

// Start the server
app.listen(PORT, () => {
    console.log(`Server is running on http://localhost:${PORT}`);
});

Start your server using the following command.

node index.js

Fundraiser Platform using MEAN

Creating a fundraiser platform using the MEAN stack (MongoDB, Express.js, Angular, and Node.js) provides a powerful combination of technologies for building dynamic and scalable web applications. In this article, we will see a step-wise process of creating the application.

Project Preview:

output preview

Similar Reads

Approach to build a fundraiser application using MEAN

Set Up Backend with Express, Node, and Mongoose.Install Express.js, Node.js, and Mongoose to create a backend server.Initialize an Express application and configure routes to handle HTTP requests.Connect to MongoDB Database.Set up a connection to MongoDB using Mongoose and define Mongoose schemas to structure and model the data for the application.Implement API Endpoints and Define routes to handle requests for creating, reading, updating, and deleting data.Use Angular CLI to generate a new Angular project.Navigate to the project directory and install required dependencies.Set Up Angular Components and Services.Create Angular components to define the user interface and structure of the application.Develop Angular services to encapsulate business logic and interact with backend APIs....

Steps to Create Server

Step 1: Create a new directory named backend....

Steps to Create the Frontend

Step 1: Setup Angular Project...

Contact Us