Skip to content

maxxunit1/backend-microservices

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

34 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Backend Microservices

Production-ready microservices architecture with Node.js, Express, and RESTful APIs. Built for scalability and maintainability.

✨ Features

  • πŸš€ RESTful API endpoints
  • πŸ” Authentication & Authorization
  • πŸ“Š Database integration
  • πŸ”„ Error handling & logging
  • πŸ§ͺ Unit tests included
  • πŸ“¦ Modular architecture
  • 🐳 Docker ready

πŸš€ Quick Start

Installation

git clone https://github.com/YOUR_USERNAME/backend-microservices.git
cd backend-microservices
npm install

Configuration

Create .env file:

PORT=3000
NODE_ENV=development
DATABASE_URL=mongodb://localhost:27017/myapp
JWT_SECRET=your-secret-key

Run

# Development
npm run dev

# Production
npm start

# Tests
npm test

πŸ“‚ Project Structure

backend-microservices/
β”œβ”€β”€ README.md          # Documentation
β”œβ”€β”€ package.json       # Dependencies
β”œβ”€β”€ server.js          # Main server
β”œβ”€β”€ routes.js          # API routes
β”œβ”€β”€ middleware.js      # Middleware functions
β”œβ”€β”€ database.js        # Database connection
└── .gitignore        # Git ignore

πŸ”Œ API Endpoints

Health Check

GET /health
Response: { status: "ok", timestamp: "..." }

Users

GET    /api/users          # Get all users
GET    /api/users/:id      # Get user by ID
POST   /api/users          # Create user
PUT    /api/users/:id      # Update user
DELETE /api/users/:id      # Delete user

Authentication

POST /api/auth/register    # Register new user
POST /api/auth/login       # Login user
POST /api/auth/logout      # Logout user

Products

GET    /api/products       # Get all products
GET    /api/products/:id   # Get product by ID
POST   /api/products       # Create product
PUT    /api/products/:id   # Update product
DELETE /api/products/:id   # Delete product

πŸ“ Usage Examples

Create User

curl -X POST http://localhost:3000/api/users \
  -H "Content-Type: application/json" \
  -d '{
    "name": "John Doe",
    "email": "john@example.com",
    "password": "secret123"
  }'

Get All Users

curl http://localhost:3000/api/users

Update User

curl -X PUT http://localhost:3000/api/users/123 \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Jane Doe"
  }'

Delete User

curl -X DELETE http://localhost:3000/api/users/123

πŸ” Authentication

Register

POST /api/auth/register
{
  "email": "user@example.com",
  "password": "password123",
  "name": "User Name"
}

Response:
{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "user": { "id": 1, "email": "user@example.com" }
}

Login

POST /api/auth/login
{
  "email": "user@example.com",
  "password": "password123"
}

Response:
{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "user": { "id": 1, "email": "user@example.com" }
}

Protected Routes

// Add Authorization header
Authorization: Bearer <token>

πŸ› οΈ Middleware

Logger Middleware

Logs all incoming requests with timestamp, method, and URL.

Error Handler

Catches and formats errors with proper status codes.

CORS Middleware

Enables Cross-Origin Resource Sharing for frontend integration.

Authentication Middleware

Validates JWT tokens for protected routes.

πŸ“Š Database

Connection

const db = require('./database');

// Connect
await db.connect();

// Query
const users = await db.query('SELECT * FROM users');

// Insert
await db.insert('users', { name: 'John', email: 'john@example.com' });

Models

// User Model
{
  id: Number,
  name: String,
  email: String,
  password: String (hashed),
  createdAt: Date,
  updatedAt: Date
}

// Product Model
{
  id: Number,
  name: String,
  description: String,
  price: Number,
  stock: Number,
  createdAt: Date
}

πŸ§ͺ Testing

# Run all tests
npm test

# Run specific test
npm test -- routes.test.js

# Coverage report
npm run test:coverage

Test Example

describe('Users API', () => {
  test('GET /api/users returns users', async () => {
    const response = await request(app).get('/api/users');
    expect(response.status).toBe(200);
    expect(response.body).toBeInstanceOf(Array);
  });
});

πŸ”§ Configuration

Environment Variables

# Server
PORT=3000
NODE_ENV=production

# Database
DATABASE_URL=mongodb://localhost:27017/myapp
DB_HOST=localhost
DB_PORT=5432
DB_NAME=myapp
DB_USER=admin
DB_PASSWORD=secret

# JWT
JWT_SECRET=your-secret-key
JWT_EXPIRES_IN=7d

# CORS
CORS_ORIGIN=http://localhost:3001

πŸ“ˆ Performance

  • Request logging with timestamps
  • Error handling with status codes
  • Database connection pooling
  • Response compression
  • Rate limiting (optional)

🐳 Docker Deployment

# Build image
docker build -t backend-microservices .

# Run container
docker run -p 3000:3000 -e DATABASE_URL=... backend-microservices

# Docker Compose
docker-compose up

πŸ”’ Security

  • Password hashing (bcrypt)
  • JWT token authentication
  • Input validation
  • SQL injection prevention
  • XSS protection
  • CORS configuration
  • Rate limiting

πŸ“š Dependencies

{
  "express": "^4.18.0",
  "cors": "^2.8.5",
  "dotenv": "^16.0.0",
  "jsonwebtoken": "^9.0.0",
  "bcrypt": "^5.1.0"
}

🀝 Contributing

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

πŸ“„ License

MIT License - see LICENSE file

πŸ™ Acknowledgments

  • Express.js team
  • Node.js community
  • Open source contributors

Made with ❀️ for backend developers

πŸ”— Related Projects

  • Frontend: frontend-components-library
  • Docker: docker-deployment-configs
  • API Testing: api-testing-framework

Update 2025-10-16 11:05:32

Modified: 2025-10-16 11:05:32

CONFIG_VALUE = 'new_value'

Update 2025-10-17 07:24:14

@decorator def enhanced_function(): """Enhanced functionality""" return improved_result()

Update 2025-10-23 17:56:05

Added validation to prevent edge case

if not input_value: return default_value return process(input_value)

Update 2025-10-27 03:10:34

Modified: 2025-10-27 03:10:34

CONFIG_VALUE = 'new_value'

Update 2025-10-28 16:23:16

@decorator def enhanced_function(): """Enhanced functionality""" return improved_result()

Update 2025-10-31 12:56:40

@decorator def enhanced_function(): """Enhanced functionality""" return improved_result()

About

Production-ready microservices architecture with Node.js and Express

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published