-
Notifications
You must be signed in to change notification settings - Fork 10
Enhance README #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,234 @@ | ||
| # root-template-express | ||
|
|
||
| Backend template for Root Kings Express Applications | ||
|
|
||
| ## Required `env` variables | ||
|
|
||
| 1. `PORT` | ||
| 2. `MONGODB_URI` | ||
| 3. `NODE_ENV` | ||
| 4. `JWT_EXPIRES` | ||
| 5. `JWT_ISSUER` | ||
| 6. `JWT_SECRET` | ||
| 7. `UPLOADS_DIR` | ||
| 8. `PUBLIC_DIR` | ||
| A secure Express.js backend template with MongoDB integration, featuring password hashing, input validation, and JWT authentication. | ||
|
|
||
|  | ||
|  | ||
|  | ||
|  | ||
|
|
||
| ## Features | ||
|
|
||
| - **Password Security** - Bcrypt hashing (10 salt rounds) | ||
| - **Input Validation** - Joi validation for emails, passwords, and user data | ||
| - **JWT Authentication** - Secure token-based auth | ||
| - **MongoDB Integration** - Mongoose ORM with pagination | ||
| - **CORS Protection** - Cross-origin request handling | ||
| - **Request Logging** - Morgan HTTP request logger | ||
| - **Email Support** - SendGrid integration ready | ||
| - **Production Ready** - Error handling and security best practices | ||
|
|
||
| ## Installation | ||
|
|
||
| ### Prerequisites | ||
| - Node.js (v14 or higher) | ||
| - MongoDB (local or cloud) | ||
| - npm or yarn | ||
|
|
||
| ### Steps | ||
|
|
||
| ```bash | ||
| # Clone the repository | ||
| git clone https://github.com/root-kings/template-express-mongodb.git | ||
|
|
||
| # Navigate to directory | ||
| cd template-express-mongodb | ||
|
|
||
| # Install dependencies | ||
| npm install | ||
|
|
||
| # Create .env file | ||
| cp .env.example .env | ||
|
|
||
| # Start the server | ||
| npm run dev | ||
| ``` | ||
|
|
||
| ## Environment Variables | ||
|
|
||
| Create a `.env` file in the root directory: | ||
|
|
||
| ```env | ||
| # Server | ||
| PORT=3000 | ||
| NODE_ENV=development | ||
|
|
||
| # Database | ||
| MONGODB_URI=mongodb://localhost:27017/root_kings | ||
|
|
||
| # JWT | ||
| JWT_EXPIRES=7d | ||
| JWT_ISSUER=root-kings | ||
| JWT_SECRET=your_super_secret_key_here | ||
|
|
||
| # File Upload | ||
| UPLOADS_DIR=uploads | ||
| PUBLIC_DIR=public | ||
| ``` | ||
|
|
||
| ## Running the App | ||
|
|
||
| ```bash | ||
| # Development mode (auto-reload with nodemon) | ||
| npm run dev | ||
|
|
||
| # Production mode | ||
| npm start | ||
|
|
||
| # Linting | ||
| npm run lint | ||
| ``` | ||
|
|
||
| Server runs on: `http://localhost:3000` | ||
|
|
||
| ## API Documentation | ||
|
|
||
| Interactive API documentation is available when running the server in development: | ||
|
|
||
| ``` | ||
| http://localhost:3000/api-docs | ||
| ``` | ||
|
|
||
| Please use the interactive docs for up-to-date endpoint details and examples. | ||
|
|
||
| ## Project Structure | ||
|
|
||
| Top-level folders: | ||
|
|
||
| ``` | ||
| controllers/ | ||
| models/ | ||
| routes/ | ||
| middlewares/ | ||
| utils/ | ||
| data/ | ||
| ``` | ||
|
|
||
| ## Security Features | ||
|
|
||
| **Password Security** | ||
| - Bcrypt hashing with 10 salt rounds | ||
| - Passwords never returned in API responses | ||
| - Passwords never included in JWT tokens | ||
|
|
||
| **Input Validation** | ||
| - Email format validation | ||
| - Password strength requirements | ||
| - User data validation | ||
| - Request sanitization | ||
|
|
||
| **Authentication** | ||
| - JWT-based token authentication | ||
| - Token expiration (configurable) | ||
| - Secure token verification | ||
|
|
||
| **Authorization** | ||
| - Role-based access control (RBAC) | ||
| - Token validation middleware | ||
| - Protected routes | ||
|
|
||
| **Other** | ||
| - CORS protection | ||
| - HTTP request logging | ||
| - Error handling | ||
| - Database connection pooling | ||
|
|
||
| ## Testing | ||
|
|
||
| ### Using REST Client (VS Code) | ||
|
|
||
| Open `data/test.http` and test endpoints: | ||
|
|
||
| ```http | ||
| ### Create User | ||
| POST http://localhost:3000/api/users | ||
| Content-Type: application/json | ||
|
|
||
| { | ||
| "name": "Test User", | ||
| "email": "[email protected]", | ||
| "password": "TestPass@123", | ||
| "type": "root" | ||
| } | ||
|
|
||
| ### Login | ||
| POST http://localhost:3000/api/auth/login | ||
| Content-Type: application/json | ||
|
|
||
| { | ||
| "email": "[email protected]", | ||
| "password": "TestPass@123", | ||
| "type": "root" | ||
| } | ||
| ``` | ||
|
|
||
| ### Using cURL | ||
|
|
||
| ```bash | ||
| # Create user | ||
| curl -X POST http://localhost:3000/api/users \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{"name":"John","email":"[email protected]","password":"TestPass@123","type":"root"}' | ||
|
|
||
| # Login | ||
| curl -X POST http://localhost:3000/api/auth/login \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{"email":"[email protected]","password":"TestPass@123","type":"root"}' | ||
| ``` | ||
|
|
||
| ## Scripts | ||
|
|
||
| | Command | Description | | ||
| |---------|-------------| | ||
| | `npm start` | Start production server | | ||
| | `npm run dev` | Start with auto-reload (nodemon) | | ||
| | `npm run lint` | Run ESLint | | ||
|
|
||
| ## Contributing | ||
|
|
||
| 1. Fork the repository | ||
| 2. Create a feature branch (`git checkout -b feat/amazing-feature`) | ||
| 3. Commit changes (`git commit -m 'feat: add amazing feature'`) | ||
| 4. Push to branch (`git push origin feat/amazing-feature`) | ||
| 5. Open a Pull Request | ||
|
|
||
| ### Before Submitting PR | ||
| - Run linter: `npm run lint` | ||
| - Test your changes thoroughly | ||
| - Update documentation if needed | ||
| - Follow the existing code style | ||
|
|
||
| ## Issues & Bugs | ||
|
|
||
| Found a bug? [Open an issue](https://github.com/root-kings/template-express-mongodb/issues) with: | ||
| - Clear description of the problem | ||
| - Steps to reproduce | ||
| - Expected vs actual behavior | ||
| - Your environment (Node version, OS, etc.) | ||
|
|
||
| ## License | ||
|
|
||
| MIT License - see LICENSE file for details | ||
|
|
||
| ## Author | ||
|
|
||
| **Root Kings** | ||
| - GitHub: [@root-kings](https://github.com/root-kings) | ||
|
|
||
| ## Acknowledgments | ||
|
|
||
| - Express.js team for the amazing framework | ||
| - MongoDB for the database | ||
| - All contributors and users | ||
|
|
||
| ## Support | ||
|
|
||
| For issues, questions, or suggestions: | ||
| - Open an issue on GitHub | ||
| - Check existing documentation | ||
| - Review the code comments | ||
|
|
||
| --- | ||
|
|
||
|
|
||
| **Made with ❤️ by Root Kings** | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i prefer the earlier heading