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
These instructions will help you run the project on your local machine.
- Clone the repository:
git clone https://github.com/goktugcy/ExpressTS-Boilerplate.git
Navigate to the project folder: cd ExpressTS-Boilerplate
Install the required dependencies:
npm install
Go to https://goktugceyhan.dev/playground or https://playcode.io/javascript and create a random value by pasting the code below. Then add this generated value to the SECRET_KEY field in your env file.
function generateSecureHexString(size) {
const array = new Uint8Array(size / 2);
window.crypto.getRandomValues(array);
return Array.from(array, byte => byte.toString(16).padStart(2, '0')).join('').toUpperCase();
}
const hexString = generateSecureHexString(45);
console.log('Generated Secure Hex String:', hexString);
Start the project:
npm run dev
Open your browser and go to http://localhost:8003 to see the "Hello World" message.
This boilerplate provides a modular route structure and includes an example of basic authentication using middleware. Let's dive into the details:
The route structure follows a modular pattern, which helps keep your code organized as your application grows. The src/routes
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 src/routes
directory and include them in the main src/app.ts
file.
The example authentication system in this boilerplate demonstrates the basic flow of user registration, login, and protected routes using middleware.
-
Registration: The
/register
endpoint inroutes.ts
allows users to register by providing a username, email, and password. Passwords are hashed before being stored in the database. -
Login: The
/login
endpoint inroutes.ts
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
authenticateMiddleware
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.
This project is licensed under the MIT License. See the LICENSE
file for more details.