There are a lot of section that goes into creating a production grade NodeJS application. In this repository I tried to gather as much as possible. This is an ExpressJS application with the following features.
- Typescript all the way
- EsLint, Prettier and Husky integration
- Docker
- Sequelize integration
- Multiple Environments
- Logging
- Error handling in a central place
- Request Validation
- Swagger API documentation
- Dependency Injection
- Setting up Testing
Let me know what else can make this repo even better
One key difference with other similar projects is that I documented every step in the way. Following is a series of articles that will guide you on how to build this project from absolute scratch.
- Creating NodeJS with Typescript from Scratch
- Adding Eslint, Prettier and Husky
- Introduce Express into the Project
- Adding Docker for Development and Production
- Setting up local database with Docker
- Error Handling
- Request Validation
- Production Grade Logging
- Environment Configuration
- Security Best Practices
- Dependency Injection
- Using Sequelize ORM
- Adding Swagger for API Documentation
The major technologies that were used to build this project are:
Here goes the instructions to get the project up and running.
To run this project You will need the following things installed on your machine
- NodeJS
- NPM
- Docker (Optional)
It's super simple. If you already have Docker installed and running on your machine you can just run
docker-compose up
It will give you 3 things
- The Express server in development mode (With hot reloading support)
- A PostgreSQL database server (If you prefer something else like MySQL just make a couple of change inside the
docker-compose.yaml
file) The credentials are
DB_HOST = database-layer;
DB_NAME = dbname;
DB_USER = dbuser;
DB_PASSWORD = dbpassword;
- A Database investigation tool named
Adminer
(You can inspect any kind of database from the browser) You can access it fromhttp://localhost:8080
If you want to change or update any code you can just make the change and from the console you will see that the server is getting updated.
If you don't use Docker then you will get an exception specifying you don't have any database. TO avoid that you can do 2 things.
- First go inside the
.env.development
file and specify the following variables of a database server that you are using.
DB_HOST=database-layer
DB_NAME=dbname
DB_USER=dbuser
DB_PASSWORD=dbpassword
- Otherwise go inside the
index.ts
file and on line number 29 comment of the following line
dbClient = await connection.sync();
If you want to add a new route then you will goto /routes
folder and add a new Router.
Then register that router in the index.ts
file under the /routes
folder.
Then you will create a Controller under the /controllers
directory.All business logics should go into there.
Specific use cases should be handles by Service classes under the /service
folder.
All Database related things should go under /repositories
folder.
To create a new model for data base look into the /models
folder.
Distributed under the MIT License. See LICENSE.txt
for more information.