- Introduction
- Features
- Prerequisites
- Installation
- Usage
- API Endpoints
- Request & Response Examples
- Error Handling
- Feedback
- Support
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.
- 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
- Node.js (version 14 or later recommended)
- npm (comes with Node.js)
-
Clone the repository:
git clone https://github.com/MuhdHanish/blog_api_json_server.git cd blog_api_json_server
-
Install dependencies:
npm install
-
Start the JSON Server and Express server together:
npm start
-
If running JSON Server separately:
json-server --watch db.json --port 3000
-
Start the Express server:
npm run start:server
The server will run on http://localhost:8000
.
/
: Server statusGET /api/blogs
: Retrieve all blogsGET /api/blogs/:id
: Retrieve a specific blogPOST /api/blogs
: Create a new blogPUT /api/blogs/:id
: Update an existing blogDELETE /api/blogs/:id
: Delete a blog*
: Catch all route for handling undefined routes
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"
}
]
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"
}
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"
}
Request:
DELETE /api/blogs/cf74
Response:
Status: 204 No Content
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.
If you have any feedback, please reach me at muhammedhanish11@gmail.com or connect with me on LinkedIn.
Show your support by 🌟 starring the project!!