Skip to content

ramkumar897003/node-express-typescript-blogs-rest-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Blog API

A secure and scalable RESTful API for a blog platform built with TypeScript, Express, and MongoDB. Features include user authentication, blog posts, comments, and comprehensive API documentation.

Core Features

  • 🔐 JWT-based Authentication & Authorization
  • 👥 User Management with Role-based Access
  • 📝 Blog Posts & Comments System
  • 📄 Pagination & Search Functionality
  • 📚 Swagger API Documentation
  • 🛡️ Security Features (XSS Protection, Rate Limiting)
  • ✅ Input Validation & Sanitization
  • 🧪 Comprehensive Test Coverage

Tech Stack

  • Language: TypeScript
  • Runtime: Node.js
  • Framework: Express.js
  • Database: MongoDB
  • Authentication: JWT
  • Documentation: Swagger/OpenAPI
  • Testing: Jest

Getting Started

Prerequisites

  • Node.js (v14+)
  • MongoDB
  • npm/yarn

Installation

  1. Clone the repository
  2. Install dependencies:
npm install
  1. Configure environment variables: Create a .env file in the root directory:
PORT=3000
MONGODB_URI=mongodb://localhost:27017/blog
JWT_SECRET=your_secret_key
JWT_EXPIRES_IN=24h

Running the Application

Development mode:

npm run dev

Production mode:

npm run build
npm start

Run tests:

npm test

API Documentation

Access the Swagger documentation at /api-docs when the server is running.

Key Endpoints

Authentication

  • POST /api/auth/register - Register new user
  • POST /api/auth/login - User login
  • GET /api/auth/verify - Verify JWT token

Users

  • GET /api/users/profile - Get user profile
  • PATCH /api/users/profile - Update profile
  • PATCH /api/users/password - Change password
  • PATCH /api/users/avatar - Update avatar

Blogs

  • GET /api/blogs - List all blogs
  • POST /api/blogs - Create blog
  • GET /api/blogs/:id - Get blog
  • PUT /api/blogs/:id - Update blog
  • DELETE /api/blogs/:id - Delete blog

Comments

  • POST /api/comments - Add comment
  • GET /api/comments/blog/:blogId - Get blog comments
  • PATCH /api/comments/:id - Update comment
  • DELETE /api/comments/:id - Delete comment

Security Features

  • ✅ JWT Authentication
  • ✅ Password Hashing (bcrypt)
  • ✅ Request Rate Limiting
  • ✅ XSS Protection
  • ✅ Security Headers
  • ✅ Input Sanitization
  • ✅ Error Handling Middleware

Project Structure

src/
├── config/         # App configuration
├── controllers/    # Request handlers
├── interfaces/     # TypeScript interfaces
├── middleware/     # Express middleware
├── models/         # Database models
├── repositories/   # Data access layer
├── routes/         # Route definitions
├── services/      # Business logic
├── utils/         # Utility functions
└── tests/         # Test suites

Error Handling

The API implements a centralized error handling system with custom error classes:

  • ValidationError: Input validation failures
  • NotFoundError: Resource not found
  • UnauthorizedError: Authentication failures
  • ForbiddenError: Permission issues

Contributing

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

License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors