Skip to content

OnoPUNPUN/Dev-Blog-Backend

Repository files navigation

Dev Blog Backend API

A robust RESTful API built with Node.js, Express, and PostgreSQL, designed to power a developer blog platform. This backend service handles user authentication, blog post management (including image uploads via Cloudinary), and categorization.

📱 Mobile Client: The companion mobile application for this backend can be found here: OnoPUNPUN/devblogs

🚀 Features

  • User Authentication: Secure registration and login using JWT (JSON Web Tokens) and bcrypt password hashing.
  • Blog Post Management: Create, read, and list blog posts.
  • Image Uploads: Seamless integration with Cloudinary for handling blog post cover images.
  • Categorization: Categorize blog posts for better content organization.
  • Database ORM: Prisma ORM for type-safe database interactions and migrations.
  • Dockerized: Fully containerized using Docker and Docker Compose for easy setup and deployment.

🛠️ Tech Stack

  • Runtime: Node.js
  • Framework: Express.js
  • Database: PostgreSQL
  • ORM: Prisma
  • Authentication: jsonwebtoken, bcryptjs
  • File Uploads: Multer, Cloudinary
  • Containerization: Docker, Docker Compose

📋 Prerequisites

Before you begin, ensure you have the following installed on your machine:

⚙️ Environment Variables

Create a .env file in the root directory and configure the following variables:

# Server
PORT=5003
NODE_ENV=development

# Database (Required for local development without Docker)
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/devblog?schema=public"

# JWT Secret
JWT_SECRET=your_super_secret_jwt_key

# Cloudinary Configuration
CLOUDINARY_CLOUD_NAME=your_cloud_name
CLOUDINARY_API_KEY=your_api_key
CLOUDINARY_API_SECRET=your_api_secret

(Note: When running with Docker Compose, the DATABASE_URL is automatically configured to point to the Postgres container).

🚀 Getting Started

Using Docker (Recommended)

The easiest way to get the project running is using Docker. This will spin up both the Node.js application and the PostgreSQL database.

  1. Ensure Docker is running.
  2. Build and start the containers:
    docker-compose up --build
  3. The API will be available at http://localhost:5003.

Running Locally (Without Docker)

  1. Install dependencies:
    npm install
  2. Start a local PostgreSQL instance and update the DATABASE_URL in your .env file.
  3. Push the Prisma schema to your database:
    npx prisma db push
  4. Start the development server:
    npm run dev

📚 API Endpoints

Health Check

  • GET / - Returns API status.

Authentication (/auth)

  • POST /auth/register - Register a new user.
    • Body: { "username": "test", "email": "test@test.com", "password": "pwd" }
  • POST /auth/login - Authenticate user and receive JWT.
    • Body: { "email": "test@test.com", "password": "pwd" }

Blogs (/blogs)

Note: All blog endpoints require a valid JWT token in the Authorization header (Bearer <token>).

  • GET /blogs - Get a list of all blogs.
  • GET /blogs/:id - Get a specific blog by ID.
  • POST /blogs - Create a new blog post. (Accepts multipart/form-data)
    • Fields: title, content, categoryIds
    • File: image (Blog cover image)

Categories (/categories)

  • GET /categories - Get all available categories.

🗄️ Database Schema

The database consists of three main models:

  • User: Stores user credentials and relates to authored blogs.
  • Blog: Stores blog content, image URLs, and relates to an Author and Categories.
  • Category: Stores blog categories for filtering and organization.

Check prisma/schema.prisma for the complete schema definition.

📝 License

This project is licensed under the ISC License.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors