This repository contains the source code for a backend application built with Node.js, Express, MongoDB, and Mongoose. The application manages blogs and users. This README provides detailed information about the project, how to set it up, and an overview of the available API endpoints.
To get started with this project, follow the steps below:
Before you begin, ensure you have the following software installed on your machine:
- Node.js
- MongoDB
-
Clone this repository to your local machine:
git clone https://github.com/aasthas2022/blogr-service
-
Change into the project directory:
cd blogr-service
-
Install project dependencies using npm:
npm install
To start the application, use the following command:
npm run start
The application will be accessible at http://localhost:3000
by default, unless you specify a different port using the PORT
environment variable.
The following section provides detailed information about the available API endpoints and their usage.
- GET
/livecheck
- This endpoint is used for basic server status checking.
-
Request:
GET /livecheck
-
Response:
{ "message": "Running!" }
-
Sample Screenshot for easier visualization:
- POST
/api/blogs
- This endpoint allows you to add a new blog to the database.
-
The request body should contain the following fields:
title
(string): The title of the blog.sub_title
(string): A subtitle for the blog.content
(string): The content of the blog.slug
(string): A unique slug for the blog.tags
(array of strings): Tags associated with the blog.author
(object): The author of the blog, with the following fields:first_name
(string): First name of the author.last_name
(string): Last name of the author.bio
(string): Author's bio.profile_pic_url
(string): URL to the author's profile picture.
-
Request:
POST /api/blogs
Request Body:
{ "title": "Example Blog Post", "sub_title": "A Subtitle for the Example Blog Post", "content": "Test Content", "slug": "example-blog-post", "tags": ["example", "blog", "post"], "author": { "first_name": "John", "last_name": "Doe", "bio": "A passionate blogger", "profile_pic_url": "https://example.com/profile-pic.jpg" } }
-
Response:
{ "_id": "unique-blog-id", "title": "Example Blog Post", "sub_title": "A Subtitle for the Example Blog Post", "content": "Test Content", "slug": "example-blog-post", "tags": ["example", "blog", "post"], "author": { "first_name": "John", "last_name": "Doe", "bio": "A passionate blogger", "profile_pic_url": "https://example.com/profile-pic.jpg" }, "created_date": "timestamp", "modified_date": "timestamp", "__v": 0 }
-
Sample Screenshot for easier visualization:
- POST
/api/users
- This endpoint allows you to add a new user to the database.
-
The request body should contain the following fields:
first_name
(string): The first name of the user.last_name
(string): The last name of the user.email
(string): The email address of the user (email validation to be added).password
(string): The user's password (password encryption to be added).bio
(string): User's bio.profile_pic_url
(string): URL to the user's profile picture.
-
Request:
POST /api/users
Request Body:
{ "first_name": "John", "last_name": "Doe", "email": "john@example.com", "password": "password", "bio": "A passionate writer", "profile_pic_url": "https://example.com/profile-pic.jpg" }
-
Response:
{ "_id": "unique-user-id", "first_name": "John", "last_name": "Doe", "email": "john@example.com", "bio": "A passionate writer", "profile_pic_url": "https://example.com/profile-pic.jpg", "__v": 0 }
-
Sample Screenshot for easier visualization:
- GET
/api/blogs
-
This endpoint lists blogs with pagination. It supports two query parameters:
page
(optional, default: 1): The page number to return.limit
(optional, default: 10): The number of blogs per page.
-
Request:
GET /api/blogs?page=2&limit=5
-
Response:
{ "totalCount": 15, "results": [ { "_id": "blog-id-1", "title": "Blog 1", // Other blog fields }, { "_id": "blog-id-2", "title": "Blog 2", // Other blog fields }, // More blog objects ], "next": { "page": 3, "limit": 5 }, "previous": { "page": 1, "limit": 5 } }
-
Sample Screenshot for easier visualization:
- GET
/api/blogs/tags/:tag
- This endpoint retrieves blogs
by a specific tag.
tag
(required): The tag to filter by.
-
Request:
GET /api/blogs/tags/example
-
Response:
[ { "_id": "blog-id-1", "title": "Example Blog 1", // Other blog fields }, { "_id": "blog-id-2", "title": "Example Blog 2", // Other blog fields }, // More blog objects ]
-
Sample Screenshot for easier visualization:
- PATCH
/api/blogs/:id
- This endpoint updates a blog by its unique ID.
id
(required): The ID of the blog to update.
- The request body should contain the fields you want to update.
-
Request:
PATCH /api/blogs/blog-id-1
Request Body:
{ "title": "Updated Blog Title", // Other fields to update }
-
Response:
{ "_id": "blog-id-1", "title": "Updated Blog Title", // Updated fields }
-
Sample Screenshot for easier visualization:
This section outlines potential future enhancements and improvements for the project:
- Add Swagger YAML file for API documentation.
- Implement unit tests for each JavaScript file.
- Add integration test cases.
- Implement field validations for API requests.
- Add route validations for robustness.
- Include a GitLab CI/CD YAML file for automated pipelines.