This project is a fully-featured Blog Management System (BMS) backend built with Node.js, Express, MongoDB, and Redis. It includes user authentication, blog creation and management, profile updates, user connections, and caching using Redis.
- User authentication (signup, signin, logout)
- Account verification and deletion
- Password reset and update
- Profile view and update
- Blog creation, update, deletion, and view
- Feed view for blogs
- User Connections (Follow/Unfollow)
- CSRF protection
- Redis caching for blog data
- Rate limiting and file upload middleware
- Json Web Token (JWT) for protected routes and token validation
- JOI for error handling
- Nodemailer for email marketing & messaging
- Pug for generating email templates
- Winston for logging
-
Clone the repository:
git clone https://github.com/reginaldsc02/bms-backend-mongodb.git cd bms-backend
-
Install dependencies:
pnpm install
-
Set up environment variables:
Create a
.env
file in the root directory and configure the following variables:# Example: EXPRESS_PORT=3001 EXPRESS_PORT=PORT_NUMBER # Example: WINSTON_SERVICE="bms-backend-mongodb" WINSTON_SERVICE=SERVICE_NAME # Example: NODE_ENV=development # Example: NODE_ENV=production NODE_ENV=ENVIRONMENT # Example: MONGO_DB_URI=mongodb://localhost:27017/BlogManagementSystem # * Replace mongodb://localhost:27017/BlogManagementSystem with your MongoDB Atlas URI to connect to Atlas. MONGO_DB_URI=MONGO_DB_URI_STRING # Example: REDIS_CLIENT_PASSWORD=enteryourredispasswordhere REDIS_CLIENT_PASSWORD=PASSWORD # Example: REDIS_CLIENT_HOST=enteryourredishosthere REDIS_CLIENT_HOST=HOST # Example: REDIS_CLIENT_PORT=30982 REDIS_CLIENT_PORT=PORT_NUMBER # Example: EXPRESS_SESSION_SECRET=secretKey EXPRESS_SESSION_SECRET=SECRET_KEY # Example: COOKIE_SESSION_SECRET=secretKey COOKIE_SESSION_SECRET=SECRET_KEY # Example: CORS_ORIGIN=http://localhost:3001 CORS_ORIGIN=ALLOWED_ORIGIN # Example: COMPANY_EMAIL="[email protected]" COMPANY_EMAIL=EMAIL # Example: COMPANY_EMAIL_PASSWORD="enteryouremailaccountpassword" COMPANY_EMAIL_PASSWORD=PASSWORD # Example: COMPANY_NAME="My Company" COMPANY_NAME=NAME # Example: WEBSITE_URL="http://localhost:5000/account-verification" WEBSITE_URL=URL # Example: JWT_SECRET_KEY=secretKey JWT_SECRET_KEY=SECRET_KEY
-
Setup Docker
sudo docker pull mongo
sudo docker run -d --name mongodb -p 27017:27017 mongo
-
Start the server:
pnpm start
The project uses environment variables for configuration. Refer to the .env.example
file for the required variables. Make sure to set these variables in your .env
file.
To start the server in development mode:
pnpm serve
If all goes well, then you should have something similar in your terminal. Refer Below:
> [email protected] preserve C:\Users\rschand\Projects\Project List\bms-backend-mongodb
> npx eslint .
> [email protected] serve C:\Users\rschand\Projects\Project List\bms-backend-mongodb
> nodemon ./server.mjs
[nodemon] 3.1.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node ./server.mjs`
info: Express Server is successfully listening! {"additional":"port: 3001","timestamp":"2024-07-26 01:03:14"}
info: Connection to MongoDB database have been established! {"timestamp":"2024-07-26 01:03:14"}
info: Connection to Redis Data Store have been established! {"additional":"port: 19314","timestamp":"2024-07-26 01:03:15"}
Use a tool like Postman, Thunder Client extension in Visual Studio Code or Insomnia to test the API endpoints. Refer to the API Endpoints section for a list of available routes.
Please click on PROJECT_STRUCTURE to view the project structure.
Alternatively you can find the PROJECT_STRUCTURE
file in the root directory of this repository and or project folder.
- POST /auth/signup - User signup
- POST /auth/signin - User signin
- POST /auth/logout - User logout (requires JWT)
- DELETE /auth/account-deletion - Delete account (requires JWT)
- POST /auth/password-reset-email - Send password reset email
- PUT /auth/account-verification - Verify account (requires token)
- PUT /auth/password-update - Update password (requires JWT)
- PUT /auth/password-reset - Reset password (requires token)
- GET /user/profile - View user profile (requires JWT)
- PUT /user/profile-update - Update user profile (requires JWT)
- PUT /user/social/operation - Create and or Update User Connections (Follow/Unfollow) (requires JWT)
- GET /blog/feeds - View blog feeds (requires JWT)
- GET /blog/:blogSlug - View specific blog (requires JWT, with blogSlug as the parameter)
- POST /:userName/blog/create - Create a new blog (requires JWT, with userName as the parameter)
- PUT /:userName/blog/:blogSlug/update - Update a blog (requires JWT, with userName as the parameter)
- DELETE /:userName/blog/:blogSlug/delete - Delete a blog (requires JWT, with userName as the parameter)
- POST /invalidate/redis/cache/blog/keys - Invalidate Redis cache for blog keys (requires JWT)
- GET /csrf - Get CSRF token
NOTE
- Token will be made available in the headers as csrf.
- Pass _csrf:
token
as a field in all HTTP request except GET.
EXAMPLE
{
"_csrf": "sLcvXE5CdwMCPJhRoill8TUKNqeZHkQd5TW1Q=",
"firstName": "Reginald",
"lastName": "Chand",
"userName": "reginaldchand",
"email": "[email protected]",
"password": "#@%4Actbvus&&8eflekdfdt53n##",
"confirmPassword": "#@%4Actbvus&&8eflekdfdt53n##"
}
Contributions are welcome! Please follow these steps to contribute:
- Fork the repository
- Create a new branch (git checkout -b feature-branch)
- Make your changes
- Commit your changes (git commit -m 'Add some feature')
- Push to the branch (git push origin feature-branch)
- Open a pull request
This project is licensed under the GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
See the License for details.
© 2024 Reginald Chand. All rights reserved.
Repository: bms-backend-mongodb
Designed & Developed with 😍 💝🌺
Kind Regards
Reginald Chand