NestJS REST API boilerplate with TypeORM, MariaDB, JWT authentication, Swagger, Docker Compose, validation, and migrations.
- Node.js compatible with the versions in
package.json - Yarn
- Docker + Docker Compose for the containerized workflow
./initThe init script will:
- copy
.env.exampleto.envwhen.envis missing; - generate a strong
JWT_SECRET_KEYwhen it is empty or still set tochangeme; - build and start the Docker Compose stack;
- wait for MariaDB to become ready;
- run TypeORM migrations.
After startup:
- API through nginx: http://localhost:8080
- Swagger docs: http://localhost:8080/api/docs
- MariaDB:
localhost:3306
For IDE autocompletion on the host machine, run:
yarn-
Install dependencies:
yarn
-
Create and edit environment config:
cp .env.example .env openssl rand -base64 32 # use this for JWT_SECRET_KEY -
Make sure MariaDB is running and that
DB_HOST,DB_PORT,DB_USERNAME,DB_PASSWORD, andDB_DATABASEmatch your local database. -
Run migrations:
yarn migration:run
-
Start the app:
yarn start:dev
Local app URL: http://localhost:3000
| Variable | Description |
|---|---|
APP_ENV |
Application environment. Use dev for local development. |
APP_URL |
Value returned by the authenticated root endpoint. |
JWT_SECRET_KEY |
Required JWT signing secret. Must be at least 32 characters. |
JWT_EXPIRATION_TIME |
Optional JWT expiration time in seconds. |
CORS_ALLOW_ORIGIN |
Comma-separated allowed origins. Required outside dev. |
DB_TYPE |
Database type. This project is configured for mariadb/mysql. |
DB_HOST |
Database host. In Docker Compose this is db. |
DB_PORT |
Database port. |
DB_USERNAME |
Database user. |
DB_PASSWORD |
Database password. |
DB_DATABASE |
Database name. |
DB_SYNC |
TypeORM synchronize flag. Keep false outside throwaway development. |
Swagger is available at /api/docs.
Main routes:
POST /api/auth/registerβ create a userPOST /api/auth/loginβ receive a JWT access tokenGET /api/auth/meβ return the authenticated userGET /β authenticated root endpoint
Use the returned JWT as a bearer token for protected routes.
TypeORM configuration lives in src/database/data-source.ts. Entities are loaded from src/**/*.entity.ts in development and dist/**/*.entity.js after build.
Create a migration:
yarn migration:create src/migrations/MigrationNameRun migrations:
yarn migration:runRevert the last migration:
yarn migration:revertInside Docker, prefix commands with docker exec -it nest, for example:
docker exec -it nest yarn migration:run
DB_SYNC=truecan be useful for throwaway local experiments, but do not use it in production because schema synchronization can cause data loss.
yarn start # run src/main.ts with ts-node
yarn start:dev # run with nodemon
yarn start:debug # run with nodemon debug config
yarn start:prod # run compiled dist/main.js
yarn test # unit tests
yarn test:watch # unit tests in watch mode
yarn test:cov # coverage
yarn test:e2e # e2e tests
yarn format # prettier over src/**/*.tsdocker-compose.ymlβ local development stack: NestJS app, nginx, MariaDB.docker-compose-swarm.ymlβ swarm-oriented deployment template.Dockerfileβ development app image.Dockerfile-prodβ production app image.Dockerfile-nginxβ nginx reverse proxy image.
- Passwords are hashed with
bcryptthrough a TypeORM value transformer. - Validation is enabled globally with Nest
ValidationPipeand a custom trim pipe. - CORS is open only in
APP_ENV=devwhenCORS_ALLOW_ORIGINis empty.