This project is a backend implementation for a blogging platform where users can write, update, and delete their blogs. The system has two main roles: Admin and User. The Admin has special permissions to manage users and their blogs, while regular users can perform CRUD operations on their own blogs. The backend also includes secure authentication, role-based access control, and a public API for viewing blogs with search, sort, and filter functionalities.
You can access the live project here.
- Authentication & Authorization: Secure login, role-based access control with Admin and User roles.
- Blog Management: Admins can manage users and blogs, while users can manage their own blogs.
- Public Blog API: Fetch blogs with search, sort, and filter functionalities.
- Error Handling: Comprehensive error handling for smooth user experience.
- Winston Logging: Logs request and response information for better debugging and tracking.
- TypeScript
- Node.js
- Express.js
- MongoDB with Mongoose
- Zod for data validation
- Winston for logging API request and response data
- JWT for authentication
- POST
/api/auth/register
Register a new user with email and password.
- POST
/api/auth/login
Login with email and password and generate a JWT token.
- POST
/api/blogs
Create a new blog (requires authentication).
- PATCH
/api/blogs/:id
Update an existing blog (only the author can update it).
- DELETE
/api/blogs/:id
Delete an existing blog (only the author can delete it).
- GET
/api/blogs
Fetch all blogs with support for search, sorting, and filtering.
- PATCH
/api/admin/users/:userId/block
Admin can block a user.
- DELETE
/api/admin/blogs/:id
Admin can delete any blog.
Standard error response format for API errors:
{
"success": false,
"message": "Error message describing the issue",
"statusCode": 400,
"error": {"details": "Additional error details, if applicable"},
"stack": "error stack trace, if available"
}
- name: Full name of the user.
- email: User's email for authentication.
- password: Encrypted password.
- role: Either "admin" or "user".
- isBlocked: Flag indicating whether the user is blocked.
- createdAt: Timestamp when the user was created.
- updatedAt: Timestamp when the user was last updated.
- title: Title of the blog.
- content: Content of the blog.
- author: Author's ID (reference to User model).
- isPublished: Flag indicating whether the blog is published.
- createdAt: Timestamp when the blog was created.
- updatedAt: Timestamp when the blog was last updated.
- Node.js
- MongoDB
- Git
-
Clone the repository:
git clone [email protected]:shahadathhs/blog-management-server.git cd blog-management-server
-
Install dependencies:
npm install
-
Create a
.env
file and configure the environment variables for MongoDB, JWT secret, etc. Check.env.example
for reference. -
Run the server:
npm run start:dev
-
The backend will be running on
http://localhost:3000
.
- Email: [email protected]
- Password: 123456