Skip to content

Node.js Express server acting as a proxy for a JSON Server, providing a RESTful API for managing blog posts.

Notifications You must be signed in to change notification settings

MuhdHanish/blog_api_json_server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Blog API JSON Server

Table of Contents

  1. Introduction
  2. Features
  3. Prerequisites
  4. Installation
  5. Usage
  6. API Endpoints
  7. Request & Response Examples
  8. Error Handling
  9. Feedback
  10. Support

Introduction

This project is a Node.js Express server acting as a proxy for a JSON Server, providing a RESTful API for managing blog posts. It offers a robust solution for creating, reading, updating, and deleting blog entries with custom error handling and logging.

Features

  • Full CRUD operations for blog posts
  • Custom error handling with detailed error messages
  • Error logging with stack traces for debugging
  • CORS support for cross-origin requests
  • TypeScript for type safety and better developer experience
  • Integration with JSON Server for easy data persistence

Prerequisites

  • Node.js (version 14 or later recommended)
  • npm (comes with Node.js)

Installation

  1. Clone the repository:

    git clone https://github.com/MuhdHanish/blog_api_json_server.git
    cd blog_api_json_server
  2. Install dependencies:

    npm install
  3. Start the JSON Server and Express server together:

    npm start

Usage

  1. If running JSON Server separately:

    json-server --watch db.json --port 3000
  2. Start the Express server:

    npm run start:server

The server will run on http://localhost:8000.

API Endpoints

  • /: Server status
  • GET /api/blogs: Retrieve all blogs
  • GET /api/blogs/:id: Retrieve a specific blog
  • POST /api/blogs: Create a new blog
  • PUT /api/blogs/:id: Update an existing blog
  • DELETE /api/blogs/:id: Delete a blog
  • *: Catch all route for handling undefined routes

Request & Response Examples

GET /api/blogs

Response body:

[
  {
    "id": "cf73",
    "title": "Introduction to Express.js",
    "content": "Express.js is a minimal and flexible Node.js web application framework...",
    "author": "John Doe",
    "createdAt": "2023-06-01T10:00:00Z",
    "updatedAt": "2023-06-01T10:00:00Z"
  }
]

POST /api/blogs

Request body:

{
  "title": "Getting Started with TypeScript",
  "content": "TypeScript is a typed superset of JavaScript that compiles to plain JavaScript...",
  "author": "Jane Smith"
}

Response body:

{
  "id": "cf74",
  "title": "Getting Started with TypeScript",
  "content": "TypeScript is a typed superset of JavaScript that compiles to plain JavaScript...",
  "author": "Jane Smith",
  "createdAt": "2023-06-02T15:30:00Z",
  "updatedAt": "2023-06-02T15:30:00Z"
}

PUT /api/blogs/:id

Request body:

{
  "title": "Updated: Getting Started with TypeScript",
  "content": "TypeScript is a powerful typed superset of JavaScript...",
  "author": "Jane Smith"
}

Response body:

{
  "id": "cf74",
  "title": "Updated: Getting Started with TypeScript",
  "content": "TypeScript is a powerful typed superset of JavaScript...",
  "author": "Jane Smith",
  "createdAt": "2023-06-02T15:30:00Z",
  "updatedAt": "2023-06-02T16:45:00Z"
}

DELETE /api/blogs/:id

Request:

DELETE /api/blogs/cf74

Response:

Status: 204 No Content

Error Handling

The API uses custom error handling. If an error occurs, the response will be in the following format:

{
  "status": "error",
  "message": "Error message here"
}

Errors are also logged to the console with stack traces for debugging purposes.

Feedback

If you have any feedback, please reach me at muhammedhanish11@gmail.com or connect with me on LinkedIn.

Support

Show your support by 🌟 starring the project!!

Releases

No releases published

Packages

No packages published