My personal boilerplate for working on Node.js + TypeScript + Express + PostgreSQL.
Originally this is made for the Research and Development (RnD) division in BNCC@Bandung. But I decided to make it open-source under my account since I was the one who made it and because I have been using it for my projects or other projects that is using this techstack.
To see the dependencies used in this boilerplate, go here.
- Entity-Relationship Diagram
- Quick Start
- Project Structure
- Commands
- Environment Variables
- Dependencies
- Make sure you have installed Yarn and PostgreSQL.
- Clone the repo
git clone https://github.com/Alviannn/express-ts-boilerplate.git
- Install the dependencies
yarn install
- Duplicate the
.env.example
file to.env
and fill the database credentials - Generate JWT secrets
yarn jwt:generate
- (Optional) If you want, you can seed data
yarn seed
- Run the dev server
yarn dev
<your project>\
|--scripts\ # User scripts for automating
|--src\ # Source folder
|--configs\ # Application configs
|--controllers\ # Routes and controllers
|--v1 # The first major version of the controllers
|--database\ # Database related code
|--entities\ # Database models/entities (represents table)
|--datasource.ts # TypeORM datasource configuration
|--internals # Internal functionalities (you will rarely touch this)
|--decorators\ # Custom decorators
|--routes\ # Server routes, provides automatic routing
|--middlewares\ # Custom middlewares
|--services\ # Business logic (service layer)
|--typings\ # Custom types/interface for type assertion
|--utils\ # Utility functions and/or classes
|--validations\ # Schemas for validating JSON requests
|--app.ts # Express app and it's configuration
|--server.ts # Program entry point (include database)
|--.eslintrc.json # ESLint config
|--tsconfig.json # TypeScript compiler config
|--...
Running:
# compiles the project to `build` directory
yarn compile
# diagnose the TS compiler
yarn compile:debug
# starts the program (must be compiled first)
yarn start
# Runs the server in Development environment (no compiled files)
yarn dev
Data seeding:
# Add a bunch of prepared data in `seeder.ts` file
yarn seed
Cleans the compiled files (in build
directory):
yarn clean
Linting:
# runs ESLint to `src` directory
yarn lint
# fixes ESLint errors (for fixable errors only)
yarn lint:fix
TypeORM:
# shows TypeORM commands
yarn typeorm -h
# shows migration status
yarn migration:show
# generates a migration based recent schema changes
yarn migration:generate <migration-name>
# creates a new migration
yarn migration:create <migration-name>
# runs all pending migrations
yarn migration:run
# reverts all migrations
yarn migration:revert
JSONWebToken:
# generate JWT secrets (both access and refresh secrets)
yarn jwt:generate
Found in the .env
file
# the JWT secrets
JWT_ACCESS_SECRET=
JWT_REFRESH_SECRET=
# access token expire time
JWT_ACCESS_EXPIRE=15m
# refresh token expire time
JWT_REFRESH_EXPIRE=30d
# the postgres database credentials
DB_HOST=
DB_PORT=5432
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
- express
- Node.js minimal backend framework.
- cors
- Middleware to enable CORS (Cross-origin resource sharing).
- Allows the frontend devs to access the backend.
- helmet
- Secures the backend HTTP headers.
- compression
- Compression middleware.
- It compresses the server response with GZIP, the client will then receive the responses in smaller size of data.
- http-status-codes
- To avoid magic numbers and use constants enum. Ex: Using
BAD_REQUEST
instead of400
.
- To avoid magic numbers and use constants enum. Ex: Using
- joi
- Library for validating JSON, making it easy to make sure all (or certain) properties exists and valid.
- luxon
- Better date and time library than the default
Date
from JS. - Why not
momentjs
? Because it has stopped it's development, check here and I personally like it.
- Better date and time library than the default
- pg
- PostgreSQL database for our backend projects, although we won't be using this directly, but through
typeorm
.
- PostgreSQL database for our backend projects, although we won't be using this directly, but through
- typeorm
- ORM (Object-relational mapping) library for Node.js.
- Helps us to access the database without a need to write SQL queries.
- It can prevent typos in SQL query.
- It can make cleaner codes.
- It's perfect for TypeScript.
- bcrypt
- Securing passwords easily, it hashes and also adds salt to it.
- It's a bad practice to store passwords in plain-text, this forum explains why it's bad.
- jsonwebtoken
- Token based user authentication, we need to know whether user is logged in or not.
- ts-node
- Able to execute TypeScript Node.js without compiling.
- It's used in development environment.
- cross-env
- Run Node.js scripts with custom environment without the need of .env.
- In Unix based OS, it's easy to achieve this even without
cross-env
, but for Windows users it's trickier.
- morgan
- A logger middleware for HTTP request.
- It logs the event for each requests.
- winston
- Flexible logger for anything and everything.
- It's the main logger with
morgan
, and it's as powerful as ever.