Dev Stories Backend is a full-featured blogging platform API with authentication, story management, comments, likes, AI-powered features, search, pagination, and robust validation checks. Built with Node.js, PostgreSQL, Knex.js, and Docker.
-
Dev Stories Frontend : Live Demo
-
Postman Documentation of Api's : Click Here
- Dev Stories Backend 🚀
- 🔹 User Management (CRUD)
- 🔐 JWT-based Authentication
- 📖 Story Management (CRUD)
- 🛡️ Middleware for Role-based Authorization
- 💬 Comments & Likes System
- 🤖 AI-powered Story Summarization & Q&A
- 🔎 Search by Username, Story Title, or Description
- 📌 Pagination, Sorting, Filtering for Users & Stories
- 🛠️ Global Error Handling & Validation
- 🧑💻 Api Documentation using Swagger.js
- 🧪 Unit & Integration Testing
- 🐳 Dockerized Deployment
Ensure you have the following installed:
- Clone the repository:
git clone https://github.com/Sumonta056/dev-stories-backend.git cd dev-stories-backend - Install dependencies:
npm install
- Configure environment variables:
cp .env.example .env # Update .env file with your configurations
- If you encounter issues during installation, ensure that your Node.js and PostgreSQL versions are compatible with the project requirements.
Update the .env file with the following variables:
PORT=3000
DB_HOST=localhost
DB_USER=your_postgres_user
DB_PASSWORD=your_postgres_password
DB_NAME=your_database_name
DB_PORT=5432
JWT_SECRET=your_jwt_secret
BCRYPT_SALT_ROUNDS=10
GOOGLE_GEMINI_API_KEY=your_gemini_api_keyRun database migrations using Knex:
npx knex migrate:latestStart the server:
npm startVisit this link
http://localhost:3000/api-docs/#/
To run the backend using Docker:
docker compose up --build- Register User:
POST /api/users - Get All Users:
GET /api/users - Get User By Id:
GET /api/users/:id - Get User By Username:
GET /api/users/username/{username} - Update User:
PUT /api/users/:id - Delete User:
DELETE /api/users/:id - User Profile:
GET /api/users/profile/{username}
- Login:
POST /api/auth
- Create Story:
POST /api/stories - Get All Stories:
GET /api/stories - Get Story by ID:
GET /api/stories/:id - Update Story:
PUT /api/stories/:id - Delete Story:
DELETE /api/stories/:id - All Stories By Username:
GET /api/stories/user/{username}
- Add Comment:
POST /api/stories/:storyId/comments - Get Comments:
GET /api/stories/:storyId/comments - Update Comment:
PUT /api/stories/{storyid}/comments/{commentId} - Delete Comment:
DELETE /api/stories/{storyid}/comments/{commentId} - Like a Story:
POST /api/stories/:storyId/likes - All Commented Stories by Username:
GET /api/stories/commented-stories/all{username} - All Likes Stories by Username:
GET /api/stories/liked-stories{username}
- Summarize Story:
POST /api/stories/ai/summary - Ask AI Question:
POST /api/stories/ai/ask
- Search Stories:
GET /api/stories/search?id=keyword&page=1&limit=10 - Paginated Users:
GET /api/users?page=1&limit=5
- Sorting Stories:
GET /api/stories?page=1&sortBy=created_at&sortOrder=desc - Filter:
GET /api/stories?page=1&limit=3&filter=mostliked
- Subscribe to Newsletter:
POST /api/newsletter/subscribe - Send Mail to all Subscriber:
POST /api/newsletter/send - Get List of all subscriber:
GET /api/newsletter/subscribers - Remove a subscriber:
DELETE /api/newsletter/subscribers/{subscriberId}
The following middleware functions are used to enhance security and enforce business logic:
- 🔑 Authentication Middleware (
authenticateToken.ts): Ensures requests are authenticated with a valid JWT token. - 🛂 Authorization Middleware (
authorizeRoles.ts): Restricts access based on user roles (Admin, Author, User). ⚠️ Error Handling Middleware (errorHandler.ts): Handles global error responses in a structured manner.- 📦 Content Negotiation Middleware: Supports JSON, XML, and YAML responses.
To maintain data integrity and security, the following validation rules are enforced:
- User Registration & Update:
username,name, andemailmust be strings.emailmust be a valid email format.passwordmust be at least 6 characters long.
- Story Creation & Update:
titleanddescriptionare required fields.titlemust be unique.
- Pagination & Search:
pageandlimitmust be positive integers.key(search parameter) must be a string.
- Comment & Like Actions:
- Users must be logged in to comment or like a story.
- Only the comment author, story author, or admin can update/delete comments.
Run unit and integration tests:
npm test📂 dev-stories-backend
┣ 📂 src
┃ ┣ 📂 controllers
┃ ┣ 📂 services
┃ ┣ 📂 routes
┃ ┣ 📂 middlewares
┃ ┣ 📂 models
┃ ┣ 📂 config
┃ ┣ 📂 utils
┃ ┗ 📜 index.ts
┣ 📜 Dockerfile
┣ 📜 docker-compose.yml
┣ 📜 .env.example
┣ 📜 package.json
┣ 📜 README.md
┗ 📜 knexfile.js
Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change. Please ensure your code adheres to the following standards:
- Follow the existing code style.
- Write tests for new features.
- Update documentation as necessary.
This project is licensed under the MIT License. See the LICENSE file for details.