This is the backend for the Blogify application, built with Node.js, Express, and Mongoose. It provides the RESTful API endpoints for managing blog posts.
Make sure you have the following installed:
- Node.js (v14 or higher)
- pnpm
- MongoDB Atlas account or a local MongoDB instance
-
Clone the repository:
git clone https://github.com/Alauddin-24434/blogify-backend.git cd blogify-backend -
Install the dependencies using pnpm:
pnpm install
Create a .env file in the root directory and add your MongoDB connection string.
.env
MONGO\_URI=your\_mongodb\_connection\_string
PORT=5000
Start the development server:
pnpm startThe server will be running at http://localhost:5000.
This project follows an MVC-like architecture to ensure separation of concerns and maintainability.
.
├── src/
│ ├── app.ts # Main Express application setup
│ ├── server.ts # Server entry point, connects to DB & starts app
│ └── app/
│ ├── controllers/ # Handles request/response logic and calls services
│ ├── routes/ # Defines API endpoints and links to controllers
│ ├── services/ # Contains business logic and interacts with models
│ ├── middlewares/ # Custom Express middleware functions
│ └── utils/ # Utility functions and helpers
├── .env # Environment variables
├── package.json # Project dependencies and scripts
└── tsconfig.json # TypeScript configuration
server.ts: The main entry point of the application. It handles the database connection and starts the Express server (app.ts).app.ts: Configures the Express application, applies middleware, and sets up global routes.app/controllers/: These files contain the logic for handling incoming requests and sending responses. They interact with services to perform business operations.app/routes/: Defines the API routes (/posts,/posts/:id, etc.) and maps them to specific controller functions.app/services/: Encapsulates the core business logic. Services interact directly with your MongoDB models (which would typically be in amodels/directory, implied by how services would use them) to perform CRUD operations and other data manipulations.app/middlewares/: Contains custom Express middleware functions, e.g., for authentication, logging, error handling, etc.app/utils/: A collection of utility functions that can be reused across different parts of the application.
| Method | Endpoint | Description |
|---|---|---|
| GET | /posts |
Get all blog posts |
| GET | /posts/:id |
Get a single post |
| POST | /posts |
Create a new post |