A simple blog application built with Node.js, Express, and MongoDB. The application allows users to create, read, and delete blog posts. Only authenticated users with a valid JWT token can create a blog, and only the user who created a blog can delete it.
- Node.js (version X.X.X or higher)
- npm (version X.X.X or higher)
- A MongoDB database (local or remote)
First, install the required dependencies for the project:
npm installCreate a .env file in the root directory of the project with the following content:
MONGODB_URI = <YOUR-MONGODB-URI>
TEST_MONGODB_URI = <YOUR-TEST-MONGODB-URI>
NODE_ENV = test
PORT = 3001
JWT_SECRET = <YOUR-JWT-SECRET>
Replace the placeholder values (<YOUR-MONGODB-URI>, <YOUR-TEST-MONGODB-URI>, <YOUR-JWT-SECRET>) with your actual MongoDB connection strings and JWT secret.
To start the application in the development environment:
npm run devTo start the application in the production environment:
npm startTo start the application in the test environment and run the tests:
npm run test-
GET
http://localhost:3001/api/posts
Retrieves all blog posts. -
POST
http://localhost:3001/api/posts
Creates a new blog post.
Note: Requires a valid JWT token in the Authorization header. -
DELETE
http://localhost:3001/api/posts/:id
Deletes a blog post by ID.
Note: Only the user who created the blog can delete it, and a valid JWT token is required.
-
GET
http://localhost:3001/api/users
Retrieves all users. -
POST
http://localhost:3001/api/users
Registers a new user.
- POST
http://localhost:3001/api/login
Authenticates a user and returns a JWT token.
The application uses JWT (JSON Web Token) for authentication. Users must include a valid JWT token in the Authorization header as a Bearer token to create or delete blog posts.
- Replace
<YOUR-MONGODB-URI>,<YOUR-TEST-MONGODB-URI>, and<YOUR-JWT-SECRET>with your actual MongoDB connection strings and JWT secret.