Welcome to the documentation for the Blog API. This API provides various endpoints for user authentication, user management, and managing blog posts.
The Blog API is built using Express.js and MongoDB. It follows RESTful principles and provides the following functionality:
- User registration and login
- User management (viewing all users, getting a user by ID, deleting a user)
- Retrieving, creating, updating, and deleting blog posts
- User authentication using username and password
To set up and run the API locally, follow these steps:
- Clone the repository:
git clone <repository-url>
- Install dependencies:
cd blog-api
npm install
- Set up environment variables:
Create a .env
file in the root directory and provide the following variables:
PORT=3000
ATLAS_KEY=<your-mongodb-uri>
SECRET_KEY=<your-secret-key>
- Start the server:
npm start
The API will be accessible at http://localhost:3000.
POST /api/auth/register
: Register a new user. Required parameters:username
andpassword
.POST /api/auth/login
: Log in an existing user. Required parameters:username
andpassword
.
GET /api/users
: Retrieve all users. Requires authentication.GET /api/users/:id
: Retrieve a specific user by ID. Requires authentication.DELETE /api/users/:id
: Delete a specific user by ID. Requires authentication.
GET /api/posts
: Retrieve all blog posts. Requires authentication.GET /api/posts/:id
: Retrieve a specific blog post by ID. Requires authentication.POST /api/posts
: Create a new blog post. Requires authentication. Required parameters:title
andcontent
.PUT /api/posts/:id
: Update an existing blog post by ID. Requires authentication. Required parameters:title
andcontent
.DELETE /api/posts/:id
: Delete a specific blog post by ID. Requires authentication.
To run the tests for the API, follow these steps:
-
Make sure the API server is
NOT
running. -
Run the tests:
npm test
This will execute the test cases and provide the test results, including any failures or errors.
Note: The tests assume a test database is set up and will not modify the production database.
Register a new user
- URL:
/api/auth/register
- Method:
POST
- Request body:
{ "username": "john_doe", "password": "password123" }
- Response:
{ "message": "User registered successfully" }
Log in an existing user
- URL:
/api/auth/login
- Method:
POST
- Request body:
{
"username": "john_doe",
"password": "password123"
}
- Response:
{
"message": "Login successful",
"token": "<access-token>"
}
Retrieve all users
- URL:
/api/users
- Method:
GET
- Headers:
- Authorization:
Bearer <access-token>
- Authorization:
- Response:
{
"users": [
{
"id": "user1",
"username": "john_doe"
},
{
"id": "user2",
"username": "jane_smith"
}
]
}
Retrieve a specific user by ID
- URL:
/api/users/:id
- Method:
GET
- Headers:
- Authorization:
Bearer <access-token>
- Authorization:
- Response:
{
"user": {
"id": "user1",
"username": "john_doe"
}
}
Delete a specific user by ID
- URL:
/api/users/:id
- Method:
DELETE
- Headers:
- Authorization:
Bearer <access-token>
- Authorization:
- Response:
{
"message": "User deleted successfully",
"userId": "user1"
}
Retrieve all blog posts
- URL:
/api/posts
- Method:
GET
- Headers:
- Authorization:
Bearer <access-token>
- Authorization:
- Response:
{
"posts": [
{
"id": "post1",
"title": "My First Blog Post",
"content": "This is the content of my first blog post."
},
{
"id": "post2",
"title": "The Journey Begins",
"content": "Welcome to my blog!"
}
]
}
Retrieve a specific blog post by ID
- URL:
/api/posts/:id
- Method:
GET
- Headers:
- Authorization:
Bearer <access-token>
- Authorization:
- Response:
{
"post": {
"id": "post1",
"title": "My First Blog Post",
"content": "This is the content of my first blog post."
}
}
Create a new blog post
- URL:
/api/posts
- Method:
POST
- Headers:
- Authorization:
Bearer <access-token>
- Authorization:
- Request body:
{
"title": "New Blog Post",
"content": "This is the content of my new blog post."
}
- Response:
{
"message": "Blog post created successfully",
"postId": "post3"
}
Update an existing blog post by ID
- URL:
/api/posts/:id
- Method:
PUT
- Headers:
- Authorization:
Bearer <access-token>
- Authorization:
- Request body:
{
"title": "Updated Blog Post",
"content": "This is the updated content of my blog post."
}
- Response:
{
"message": "Blog post updated successfully",
"postId": "post1"
}
Delete a specific blog post by ID
- URL:
/api/posts/:id
- Method:
DELETE
- Headers:
- Authorization:
Bearer <access-token>
- Authorization:
- Response:
{
"message": "Blog post deleted successfully",
"postId": "post1"
}
That's it! You're now ready to use and test the Blog API. Refer to the endpoints section for detailed information on each API route.
Happy coding!
Feel free to customize and enhance the documentation further to fit your specific API implementation and requirements.