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:
Step 5: Create a file ‘index.js’ and set up the server.
//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: