Blood Bank Management System Project with Source Code | Creating a Team Members API in Node.js | React & Node JS Project Tutorial | Part 6

Learn how to create a fully functional Blood Bank Management System using modern technologies. This step-by-step guide is perfect for both beginners and experienced developers, offering source code and practical insights. With a focus on React JS, Node JS, and Bootstrap, you'll develop an efficient system for managing blood donations, inventory, and donors. Start building your blood bank management system today!

Technologies Used

  • React JS
  • Node JS
  • Bootstrap
  • HTML & CSS

Basic Requirement

  1. Node JS
  2. Editor
  3. Web browser

Creating a Team Members API in Node.js

In this blog, we will learn how to create a RESTful API for managing team members using Node.js, Express, and Sequelize. This API will allow us to perform CRUD operations on a team member's database.

Prerequisites

Before we begin, make sure you have the following installed:

  • Node.js
  • NPM (Node Package Manager)
  • A code editor (e.g., Visual Studio Code)
  • A MySQL or PostgreSQL database

Step 1: Create the TeamMember Model

In the models/ folder, create a file called TeamMember.js. This will define the TeamMember model with columns id, name, img, and status.

Add the following code to TeamMember.js:

            const { Sequelize, DataTypes } = require('sequelize');
            const sequelize = new Sequelize('database', 'username', 'password', {
                host: 'localhost',
                dialect: 'mysql', // or 'postgres'
            });
            
            // Define the TeamMember model
            const TeamMember = sequelize.define('TeamMember', {
                id: {
                    type: DataTypes.INTEGER,
                    primaryKey: true,
                    autoIncrement: true,
                },
                name: {
                    type: DataTypes.STRING,
                    allowNull: false,
                },
                img: {
                    type: DataTypes.STRING,
                    allowNull: true,
                },
                status: {
                    type: DataTypes.STRING,
                    allowNull: false,
                },
            });
            
            // Sync the model with the database
            (async () => {
                await sequelize.sync();
            })();
            
            module.exports = TeamMember;
                    

Step 2: Sync the Model with the Database

In your app.js or server entry file, ensure that Sequelize synchronizes the model with the database. This creates the table if it doesn’t exist.

const express = require('express');
const sequelize = require('./config/database');
const Service = require('./models/service');
const TeamMember = require('./models/TeamMember');

const app = express();

sequelize.sync()
    .then(() => {
        console.log('Database & tables synced!');
    })
    .catch(err => {
        console.error('Error syncing database:', err);
    });

app.listen(4000, () => {
    console.log('Server is running on http://localhost:4000');
});
        

Step 3: Create a GET API to Fetch TeamMember

Now let’s create a simple GET API to fetch the list of TeamMember. In the routes/ folder, create a file called TeamMemberRoutes.js:

const express = require('express');
const router = express.Router();
const TeamMember = require('./models/TeamMember');

router.get('/team-members', async (req, res) => {
    try {
        const members = await TeamMember.findAll();
        res.json(members);
    } catch (error) {
        res.status(500).json({ error: 'Internal Server Error' });
    }
});

module.exports = router;
        

Include this route in your main app.js file:

const express = require('express');
const sequelize = require('./config/database');
const serviceRoutes = require('./routes/serviceRoutes');
const TeamMemberRoutes = require('./routes/TeamMemberRoutes');

const app = express();

sequelize.sync();

app.use(express.json());

app.use('/api', serviceRoutes);
app.use('/api', TeamMemberRoutes);

app.listen(4000, () => {
    console.log('Server is running on http://localhost:4000');
});
        

Step 4: Test the GET API

Run your Node.js server:

npm start
        

Visit http://localhost:4000/api/team-members in your browser or use Postman to make a GET request and see all the available team members from the database.