- This is a simple blog API created with Node.js and Express.
- The API allows users to perform CRUD (create, read, update, delete) operations on blog posts and comments.
- It also includes JWT authentication with Passport.js.
- See the blog website with published posts built with Next.js: Blog NextJS.
- There is also a content management system too: Blog CMS.
To get started with the API, follow these steps:
- Clone the repository to your local machine.
- Run npm install to install the project dependencies.
- Rename .env.example file to .env and fill in the necessary environment variables.
- Run npm start to start the server.
The API has the following endpoints:
- GET /posts: Get all blog posts.
- POST /posts: Create a new blog post.
- GET /posts/:postId: Get a specific blog post by ID.
- PUT /posts/:postId: Update a specific blog post by ID.
- DELETE /posts/:postId: Delete a specific blog post by ID.
- GET /posts/:postId/comments: Get all comments for a specific blog post.
- POST /posts/:postId/comments: Create a new comment for a specific blog post.
- GET /posts/:postId/comments/:commentId: Get a specific comment for a specific blog post by ID.
- PUT /posts/:postId/comments/:commentId: Update a specific comment for a specific blog post by ID.
- DELETE /posts/:postId/comments/:commentId: Delete a specific comment for a specific blog post by ID.
- POST /login: Log in a user and return a JWT token.
The following dependencies are used in this project:
- express: Web framework for Node.js
- mongoose: Object modeling tool for MongoDB
- dotenv: Loads environment variables from a .env file
- express validator: Runs back-end validation for form data
- luxon: Deal with timestamp
- passport: Authentication middleware for Node.js
- passport-jwt: Passport strategy for authenticating with JWT tokens
- jsonwebtoken: JSON Web Token implementation for Node.js
The following environment variables are used in this project:
- PORT: The port number to run the server on.
- MONGO_URL: The URI for the MongoDB database.
- JWT_SECRET: The secret key used to sign JWT tokens.
- ADMIN_USERNAME: The username of admin for authenticating with JWT.
- ADMIN_PASSWORD: The password of admin for authenticating with JWT.
Contributions are welcome! If you'd like to contribute to this project, please fork the repository and submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.