.vscode | ||
data | ||
image_files/fsroot | ||
src | ||
test | ||
.dockerignore | ||
.eslintrc.js | ||
.gitignore | ||
.gitlab-ci.yml | ||
.prettierrc | ||
docker-compose.db.yml | ||
docker-compose.yml | ||
Dockerfile | ||
Dockerfile.dev | ||
nest-cli.json | ||
package.json | ||
README.md | ||
sample.env-app | ||
sample.env-docker-compose | ||
tsconfig.build.json | ||
tsconfig.json | ||
yarn.lock |
Basic Nestjs Application/API Boilerplate project with Git CI
This project is based on the Basic Docker Project with Git CI project and include:
- Node.js + Nestjs framework + Typescript + Swagger + JSON Logging (Shellscript + Winston)
- This project is based on the new project generated by
nest new <project>
command (as of 2023-03-24) - Two dockerfile for production and development (which will be supported by
.gitlab-ci.yml
) - Dockerfile using multiple-stage builds
- docker-compose setup for local development
- Minimal API implementation (version, healthcheck)
Local development using docker-compose and attach with VSCode
Preparation
Assume you are using Linux (Mac or WSL2) and your login user id is 1000
- Clone the project
- Prepare the
.env
(for docker-compose) and.env-app
(for Node.js app) files - Refer to
sample.env-*
for the details - Create data folder (for vscode-server which will be running inside the container for VSCode)
mkdir -p ./data/vscode-server
- The VSCode extension and some required data will be saved in this persistence folder.
- If you have started the service before making the directory (under your user account), you may get error when you attach VSCode. To resolve it, change the
data
andvscode-server
folder ownership to you. - Build the image
docker-compose build
Start the environment as a dummy-daemon service
Assume you have installed docker extension in VSCode
docker-compose up -d
Attach container with VSCode
- Start the service (if not yet)
- Start VSCode and locate Docker in the left tab panel, click the docker icon
- Locate the running container of your application (e.g.
basic-nestjs-app
) - Right-click the container instance, select
Attach Visual Studio Code
- From the dropdown menu, select the instance again
- A new VSCode Window will be shown if success
- If it is the first time, please pick the working folder to
/home/node/app
Run the server application
- In the VSCode terminal (within the container), type
yarn start
oryarn start:dev
- To use the VSCode debugger, click the debug and launch the
Debug Nest Framework
(Seelaunch.json
) for details
Swagger
The Nest Swagger framework is added into this project. For details, please see Nestjs OpenAPI documentation.
When the is started and either NODE_ENV
is not set to production
OR ENABLE_SWAGGER
is true
, the swagger will be enabled. It can be access via URL: /api
. For example: http://localhost:3000/api
About the Winston Logger and Price Service Core
To use the Node.js Logger with Winston and output as a json format (required by Price's K8s best practice), please consider to use Price Service Core's Utility Service for logger.
The Price Service Core has benn installed in this project. This is the command used:
yarn add --registry https://repo-manager.price-hk.com/repository/npm-group/ @price/service-core@0.4.0
And a wrapper class WinstonLoggerService
(file src/logger/winston.service.ts
) is created as well. Logger configration has also implemented in this project too.
To use this, in your TS source code:
- import the class. e.g.:
import { WinstonLoggerService } from './logger/winston.service';
- Set the context in constructor:
constructor(private logger: WinstonLoggerService) { this.logger.setContext(this.constructor.name); }
- Log as needed:
this.logger.debug("the message to log");
Working with the sample user-API (CURD)
API-Doc: /api
- Example: http://localhost:3000/api
Note:
- The following use Linux cURL command to operate the API.
- Header is provided. Especially for
POST
andPATCH
Create User
curl -X POST -H 'Content-Type: application/json' -d '{ "name": "Tester 1" }' 'http://localhost:3000/users'
List Users
List users (with header outpu):
curl -i 'http://localhost:3000/users'
Formatted result with JQ:
curl -s 'http://localhost:3000/users' | jq
Get specific user
curl -s 'http://localhost:3000/users/0' | jq
Update specific user
curl -X PATCH -H 'Content-Type: application/json' -d '{ "name": "Tester 2" }' 'http://localhost:3000/users/1'
Note: Header must be provided.
Delete specific user
curl -X DELETE 'http://localhost:3000/users/1'