https://roadmap.sh/projects/blogging-platform-api
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.
Make sure you have the following installed:
- Node.js (v14 or higher)
- PostgreSQL (v12 or higher)
-
Clone the repository:
git clone <repository-url> ex: https://github.com/NTSang171204/Restful-API.git cd <repository-folder>
-
Install dependencies of project:
npm install
-
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)
-
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() );
-
Start the server:
npm start
-
The server will run on the port specified in the
.env
file (default:3000
).
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 /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 /posts/:id
Response:
- 200: Blog post data
- 404: Blog post not found
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 /posts/:id
Response:
- 204: Blog post deleted
- 404: Blog post not found
You can test the API using tool(s) like:
- Ensure the PostgreSQL server is running and properly configured.
- Log errors for debugging, but do not expose sensitive information in responses.