Skip to content

Gaurav-3002/crud-api-server

Repository files navigation

CRUD API Server

A complete, ready-to-run CRUD API server built with Node.js, Express, and Sequelize ORM, featuring a React frontend for full-stack functionality.

🚀 Project Overview

This project demonstrates a full-stack application with:

  • Backend: RESTful API with complete CRUD operations
  • Frontend: React-based user interface with Tailwind CSS
  • Database: PostgreSQL with Sequelize ORM
  • Testing: Comprehensive test suite with Jest and Supertest
  • Documentation: Complete API documentation

🛠 Tech Stack

Backend

  • Node.js (LTS) - JavaScript runtime
  • Express.js - Web application framework
  • Sequelize - Promise-based ORM for SQL databases
  • PostgreSQL - PostgreSQL database
  • Jest + Supertest - Testing framework

Frontend

  • React - User interface library
  • Axios - HTTP client for API calls
  • Tailwind CSS - Utility-first CSS framework

📋 Prerequisites

Before running this project, ensure you have:

  • Node.js (v16 or higher)
  • npm or yarn package manager

🚀 Setup Instructions

1. Clone the Repository

git clone <repository-url>
cd crud-api-server

2. Backend Setup

cd backend

# Install dependencies
npm install

# Copy environment variables
cp .env.example .env

# Run database migrations
npm run migrate

# Start the development server
npm run dev

3. Frontend Setup

cd frontend

# Install dependencies
yarn install

# Start the development server
yarn start

4. Verify Installation

Backend Health Check:

curl http://localhost:8001/health

Access Frontend: Open your browser and navigate to http://localhost:3000

🧪 How to Run Tests

Backend Tests

cd backend
npm test

Test Coverage

The test suite includes:

  • ✅ All CRUD operations (Create, Read, Update, Delete)
  • ✅ Input validation and error handling
  • ✅ Success and error scenarios
  • ✅ Database operations and constraints

📊 API Endpoints

Method Endpoint Description
GET /health Server health check
GET /api/items Retrieve all items
GET /api/items/:id Retrieve specific item
POST /api/items Create new item
PUT /api/items/:id Update existing item
DELETE /api/items/:id Delete item

For detailed API documentation, see API_DOCS.md.

🗂 Project Structure

├── backend/                 # Express.js backend
│   ├── src/
│   │   ├── config/         # Database configuration
│   │   ├── models/         # Sequelize models
│   │   ├── routes/         # API routes
│   │   ├── controllers/    # Route controllers
│   │   └── app.js          # Express application
│   ├── tests/              # Test files
│   ├── server.js           # Server entry point
│   └── package.json        # Dependencies and scripts
├── frontend/               # React frontend
│   ├── src/
│   │   ├── App.js          # Main React component
│   │   └── App.css         # Styling
│   └── package.json        # Dependencies and scripts
├── API_DOCS.md            # API documentation
└── README.md              # This file

🐳 How to Deploy

Option 1: Heroku Deployment

  1. Prepare for Heroku:
# Add Heroku Postgres addon
heroku addons:create heroku-postgresql:mini

# Set production environment variables
heroku config:set NODE_ENV=production
heroku config:set DB_DIALECT=postgres
  1. Update package.json:
{
  "scripts": {
    "postbuild": "npm run migrate",
    "start": "node server.js"
  }
}
  1. Deploy:
git add .
git commit -m "Ready for deployment"
git push heroku main

Option 2: Docker Deployment

  1. Create Dockerfile:
FROM node:18-alpine

WORKDIR /app

# Backend setup
COPY backend/package*.json ./backend/
RUN cd backend && npm install

# Frontend setup  
COPY frontend/package*.json ./frontend/
RUN cd frontend && npm install

COPY . .

# Build frontend
RUN cd frontend && npm run build

# Run migrations and start server
EXPOSE 8001
CMD cd backend && npm run migrate && npm start
  1. Build and run:
docker build -t crud-api-server .
docker run -p 8001:8001 crud-api-server

🔧 Development Scripts

Backend

npm start          # Start production server
npm run dev        # Start development server with hot reload
npm test           # Run test suite
npm run migrate    # Run database migrations

Frontend

yarn start         # Start development server
yarn build         # Build for production
yarn test          # Run React tests

🌟 Features

Backend Features

  • ✅ Complete CRUD operations
  • ✅ Input validation with express-validator
  • ✅ Comprehensive error handling
  • ✅ Database migrations and models
  • ✅ CORS support for cross-origin requests
  • ✅ Security headers with Helmet
  • ✅ Structured JSON responses
  • ✅ Extensive test coverage

Frontend Features

  • ✅ Modern React with Hooks
  • ✅ Responsive design with Tailwind CSS
  • ✅ Real-time CRUD operations
  • ✅ Form validation and error handling
  • ✅ Loading states and user feedback
  • ✅ Confirmation dialogs for destructive actions
  • ✅ Auto-refresh and manual refresh options

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📞 Support

If you encounter any issues or have questions:

  1. Check the API Documentation
  2. Review the test files for usage examples
  3. Open an issue on GitHub

Built using Node.js, Express, React, and Sequelize

About

Full-stack CRUD API server with a Node.js and Sequelize (PostgreSQL) backend and a React + Tailwind CSS frontend.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors