Welcome to the boilerplate for building a scalable Express-based REST API written in TypeScript. This project integrates MongoDB as the database, JWT for authentication, and Role-Based Access Control (RBAC) for authorization.
- TypeScript: Strongly typed JavaScript for improved code quality.
- Express.js: Minimalist web framework for building APIs.
- MongoDB: NoSQL database with Mongoose ODM for schema modeling.
- Authentication & Authorization:
- JSON Web Tokens (JWT) for stateless authentication.
- Role-Based Access Control (RBAC) for fine-grained access control.
- Error Handling: Centralized error handling for consistent API responses.
- Environment Configuration: Easy-to-manage
.env
file for environment-specific variables. - Modular Architecture: Organized folder structure for scalability and maintainability.
- Node.js (>= 20.x)
- npm or yarn
- MongoDB (>= 8.x)
-
Clone the Repository:
git clone https://github.com/syedammar/rest-api-nodejs-typescript.git cd your-repo-name
-
Install Dependencies:
npm install
-
Environment Configuration: Copy the
.env.example
to.env
and update the values as required:cp .env.example .env
-
Run the Application:
npm run dev
- Development:
npm run dev
- Build:
npm run build
- Production Start:
npm start
- Testing:
npm test
src/
|-- config/ # Configuration files (e.g., DB, JWT)
|-- controllers/ # API controllers
|-- middlewares/ # Middleware functions
|-- models/ # Mongoose models
|-- routes/ # API routes
|-- server.ts # Server entry point
- POST
/api/auth/register
- Register a new user. - POST
/api/auth/login
- Authenticate user and get a JWT.
- GET
/api/users/:id
- Get user details by ID. - DELETE
/api/users/:id
- To Delete a user (only admin) - PUT
/api/users/:id
- To Update a user
- GET
/api/users/protected
- Accessible only to authenticated users.
- Framework: Express.js
- Language: TypeScript
- Database: MongoDB
- Authentication: JWT
- Authorization: Role-Based Access Control (RBAC)
- Fork the repository.
- Create a new branch for your feature (
git checkout -b feature-name
). - Commit your changes (
git commit -m 'Add some feature'
). - Push to the branch (
git push origin feature-name
). - Open a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
Feel free to open an issue or submit a pull request if you have any suggestions or improvements. Happy coding! 🎉