Skip to content

NTSang171204/Restful-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

https://roadmap.sh/projects/blogging-platform-api

Blog API - README

Overview

This is a simple RESTful API for a personal blogging platform. It supports basic CRUD operations (Create, Read, Update, Delete) and allows filtering posts by a search term. The API is built with Node.js, Express, and PostgreSQL.

Prerequisites

Make sure you have the following installed:

  • Node.js (v14 or higher)
  • PostgreSQL (v12 or higher)

Installation

  1. Clone the repository:

    git clone <repository-url> ex: https://github.com/NTSang171204/Restful-API.git
    cd <repository-folder>
  2. Install dependencies of project:

    npm install
  3. Set up environment variables: Create a .env file in the root directory and add the following:

    DB_USER=your_postgres_username
    DB_HOST=localhost
    DB_NAME=your_database_name
    DB_PASSWORD=your_postgres_password
    DB_PORT=5432
    PORT=3000 (if you don't have this line, it will auto is port 3000)
  4. Initialize the database: Create a posts table in your PostgreSQL database:

    CREATE TABLE posts (
        id SERIAL PRIMARY KEY,
        title TEXT NOT NULL,
        content TEXT NOT NULL,
        category TEXT NOT NULL,
        tags JSONB,
        created_at TIMESTAMP DEFAULT NOW(),
        updated_at TIMESTAMP DEFAULT NOW()
    );

Running the Application

  1. Start the server:

    npm start
  2. The server will run on the port specified in the .env file (default: 3000).

API Endpoints

Create a Blog Post

POST /posts

Request body:

{
  "title": "My Blog Post",
  "content": "This is the content of the blog post.",
  "category": "Technology",
  "tags": ["Tech", "Programming"]
}

Response:

  • 201: Blog post created
  • 400: Validation errors

Get All Blog Posts

GET /posts

Optional query parameter:

  • term: A search term to filter posts by title, content, or category (e.g., /posts?term=tech).

Response:

  • 200: Array of blog posts

Get a Single Blog Post

GET /posts/:id

Response:

  • 200: Blog post data
  • 404: Blog post not found

Update a Blog Post

PUT /posts/:id

Request body:

{
  "title": "Updated Title",
  "content": "Updated content.",
  "category": "Technology",
  "tags": ["Updated", "Tags"]
}

Response:

  • 200: Updated blog post
  • 400: Validation errors
  • 404: Blog post not found

Delete a Blog Post

DELETE /posts/:id

Response:

  • 204: Blog post deleted
  • 404: Blog post not found

Testing the API

You can test the API using tool(s) like:

Notes

  • Ensure the PostgreSQL server is running and properly configured.
  • Log errors for debugging, but do not expose sensitive information in responses.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published