Express Typescript Boilerplate Backend
This project is a basic template built using Express.js and TypeScript , MongoDB. Modular route structures.
Tech Stack :
ExpressTS Boilerplate
This project is a basic template built using Express.js and TypeScript. It includes the following features:
- Usage of Express.js
- Development with TypeScript
- Modular route structures
- Example of a basic auth service
- MongoDB integration
Getting Started
These instructions will help you run the project on your local machine.
- Clone the repository:
Install the required dependencies:
Start the project:
Open your browser and go to http://localhost:5002 to see the "Hello World" message.
Routes and Authentication
This boilerplate provides a modular route structure and includes an example of basic authentication using middleware. Let's dive into the details:
Route Structure
The route structure follows a modular pattern, which helps keep your code organized as your application grows. The directory contains individual route modules, each handling specific routes and their associated logic.
To define new routes or extend existing ones, you can create additional route modules in the directory and include them in the main file.
Authentication
The example authentication system in this boilerplate demonstrates the basic flow of user registration, login, and protected routes using middleware.
-
Registration: The endpoint in allows users to register by providing a username, email, and password. Passwords are hashed before being stored in the database.
-
Login: The endpoint in handles user logins. It checks the provided credentials against the stored hashed password and issues a JSON Web Token (JWT) upon successful login.
-
Protected Routes: The boilerplate includes of a protected routes that requires authentication. The middleware is applied to this route to ensure that only authenticated users can access it. If a user is not authenticated, they will receive a 401 Unauthorized response.