This repository serves as a template for creating a TypeScript-based Node.js backend with a Postgres database. It includes configurations for various tools and technologies to help you get started quickly with building a robust backend for your projects.
- Description
- Getting Started
- RUN in local with docker-compose up
- Project Structure
- Scripts
- Dependencies
- Environment Variables
- License
This template provides a solid foundation for a TypeScript-based Node.js backend integrated with a Postgres database. It includes configurations for ESLint, Husky, Prettier, and Swagger to ensure code quality, formatting, and documentation consistency. Additionally, it comes with Jest for testing, Flyway for database migrations, and Docker Compose for easy deployment.
Before you begin, make sure you have the following installed on your system:
- Clone the repository:
git clone https://github.com/pierrot498/template-nodejs-typescript-postgres.git
cd template-nodejs-typescript-postgres- Install the dependencies:
npm install- Set up the environment variables:
Copy the .env.example.local file and rename it to .env.local. Then, fill in the required environment variables:
LOG_DIR="../logs"
DB_HOST=your_database_host
DB_PORT=your_database_port
DB_USER=your_database_username
DB_PASSWORD=your_database_password
DB_DATABASE=your_database_name
SECRET=your_secret
Make sure to replace your_database_host, your_database_port, your_database_username, your_database_password, and your_database_name with the appropriate values.
- Navigate to the project directory:
cd template-nodejs-typescript-postgres- Start the application using
docker-compose:
docker-compose upThis will set up the necessary containers and run the project locally.
To interact with the API, you can use Postman with the correct "local" environment:
-
Import the "local" environment under
./postman/environment/local.postman_environment.jsonfile into Postman. The environment file should contain variables likeBASE_URL,JWT_TOKEN, etc. -
Import collection called
Postman template collection.postman_collection.json -
Call POST
/signupto sign up as a new client using the provided endpoint. -
Once signed up, obtain the JWT token from the response of the
/loginendpoint to get a JWT token. -
Set the obtained JWT token as the value of the
JWT_TOKENvariable in the Postman environment. -
Now, you can perform API requests with the authenticated client and create users related to that particular client.
The project structure is organized as follows:
config: Contains configuration files for various tools (ESLint, Prettier, etc.).exceptions: Holds custom exception classes for error handling.interfaces: Includes TypeScript interfaces for data structures.main/resources/db/migrations: Stores Flyway database migration scripts.middlewares: Contains custom middleware functions.routes: Defines Express routes for different API endpoints.schemas: Includes JSON schemas for validating request and response data.services: Contains business logic and interaction with the database.tests/endpoints: Houses integration tests for API endpoints.
npm start: Builds the project and starts the server.npm run dev: Builds the project and starts the server in development mode using Nodemon.npm run build: Compiles the TypeScript code using SWC and generates thedistfolder.npm run lint: Lints the TypeScript code using ESLint.npm run lint:fix: Fixes linting errors automatically.npm test: Runs Jest tests with coverage and various options.npm run unit:test: Runs Jest unit tests and generates coverage reports.
The project utilizes several dependencies, including Express, Postgres, Jest, Axios, and others. You can find the complete list of dependencies and devDependencies in the package.json file.
The project requires specific environment variables for configuration. Refer to the Installation section for details on setting up the .env.local file.
Here's the nginx load balancer diagram :
- We have created a new service named
nginx, which will act as the load balancer. - The Nginx configuration file
nginx.confis assumed to be present in the./nginxdirectory. It is mounted into the Nginx container at the path/etc/nginx/nginx.conf. The configuration innginx.confshould include the load balancer settings, directing requests to the twoapiservices (apiandapi2). - The
depends_onsection for thenginxservice ensures that the load balancer starts after theapiandapi2services are up and running.
Please ensure you have the appropriate Nginx configuration (nginx.conf) in the ./nginx directory before running the docker-compose file. The nginx.conf file should contain the necessary load balancing settings for Nginx to distribute incoming requests between the api and api2 services.
This project is licensed under the ISC License. See the LICENSE file for details.
Feel free to use this template as a starting point for your Node.js projects. Happy coding! 🚀


