This project is a Blog Management System built using Node.js, Express, and MongoDB. It provides functionalities for user authentication, blog management, and category management.
- Installation
- Running the Application
- Environment Variables
- Project Structure
- API Endpoints
- Seeding the Database
-
Initialize the project:
npm init -y
-
Install the required dependencies:
npm install express mongoose dotenv morgan cookie-parser cors
-
Install development dependencies:
npm install --save-dev nodemon
-
Create a
.gitignore
file and addnode_modules
to it:node_modules
-
Create an
index.js
file and set up the server (see below for details).
To run the application in development mode, use:
npm run dev
To run the application in production mode, use:
npm start
Update the scripts section in package.json:
"scripts": {
"dev": "nodemon index.js",
"start": "node index.js"
}
Environment Variables
Create a .env file in the root directory and add the following environment variables:
PORT=8000
DB_URL=your_mongodb_connection_string
COOKIE_SECRET=your_cookie_secret
NODE_ENV=development
Project Structure
backend-sir/
├── modules/
│ ├── blogs/
│ │ ├── blog.controller.js
│ │ ├── blog.model.js
│ │ └── blog.route.js
│ ├── users/
│ │ ├── user.controller.js
│ │ ├── user.model.js
│ │ └── user.route.js
├── routes/
│ └── index.js
├── seed/
│ └── index.js
├── .env
├── .gitignore
├── index.js
├── package.json
└── README.md
API Endpoints
User Routes
Register User: POST /api/v1/users/register
Login User: POST /api/v1/users/login
Get User by ID: GET /api/v1/users/:id
Update User: PUT /api/v1/users/:id
Delete User: DELETE /api/v1/users/:id
Blog Routes
Create Blog: POST /api/v1/blogs
Get Blogs: GET /api/v1/blogs
Get Blog by ID: GET /api/v1/blogs/:id
Update Blog: PUT /api/v1/blogs/:id
Delete Blog: DELETE /api/v1/blogs/:id
Miscellaneous Routes
Server Wakeup: GET /server-wakeup
Seeding the Database
To seed the database with initial data, run the following command:
node seed/index.js
This will connect to the MongoDB database and populate it with sample blog data.
Logging
The application uses morgan for logging HTTP requests. Logs are displayed in the console.
Middleware
CORS: Configured to allow requests from specific origins.
Cookie Parser: Used to parse cookies in the request.
JSON Parsing: Configured to parse JSON request bodies.
Error Handling
Errors are caught and returned as JSON responses with a status code of 500.
License
This project is licensed under the MIT License.