Following the NestJS recommendation for the server configuration, the server package comes with a convenient ConfigModule
declared as a @Global()
module so any other module can access it without needing to include it in its dependencies.
This module adapts to both local dotenv
files and container orchestration config volumes — e.g. Kubernetes config-volume
. It follows the below logic to define the configuration variables, in order of priority:
-
If both
CONFIG_PATH
andSECRETS_PATH
environment variables are defined, it will look for their corresponding values as folders and hydrate the configuration with their file content. -
If the
NODE_ENV
environment variable is set, it will read both the usual configurations variables and the secrets from the correspondingenv/${NODE_ENV}.env
file. All the secrets need to be prefixed with theSECRET_
string as per theconfigSchema
. -
If none of the above is provided, it will try to read the configuration and secret variables from the
env/local.env
file. -
If none of the above is satisfied, the application will throw an uncaught error and crash.
In order to be make sure that the application won't crash because of a misconfiguration once started, the configuration is entirely validated against the schema provided in the ConfigService
's configSchema
private property thanks to the @hapi/joi package.
As the ConfigModule
is declared as @Global()
, you don't need to include in the module's imports
array to use it. Nevertheless, you'll need to include the ConfigService
in the constructor arguments of any class using it. You can see an example in the HelloService.
In order to enforce type safety when using the configuration variables across the application source, you should always access them via getters. You can find examples of such typed getters at the end of the ConfigService
.
The only exception for this is in the main's bootstrap
function which needs to get the configuration variables without having access to the ConfigService
instance and which gets over the TS private
restriction by calling the envConfig
as a plain object.
By default, the server is listening to the http://localhost:4000 port. This behaviour can be configured via the local.env file.
To run the development server locally, you can use the bellow commands:
# Runs the current version of the server
yarn start
# Runs the current version of the server
# + watches for file changes
yarn start:dev
# Runs the current version of the server
# + watches for file changes
# + opens a debugger connection (default port 9229)
yarn start:debug
The powerful debug feature allows any Node.js debugger tool to connect to the running process in order to define breakpoints, log points, and more. Any Chromium based browser comes with the Node inspector feature built-in. To learn more about the Node.js debugging tools, the Node.js documentation is a nice starting point.
If you use the VS Code IDE, this repository already ships a configuration to attach
a debugger to the running application. See the .vscode/launch.json file for more information.
Once you are happy with your code, you can run a production version following these steps:
-
Build a production bundle into a brand new
dist
folder:yarn build
-
Run the production bundle:
yarn start:prod
Nest comes with Jest
and Supertest
testing frameworks to ease the testing process. Here are the different test scripts which you can run:
# Runs the unit tests
yarn test
# Runs the unit test
# + watches for file changes
yarn test:watch
# Runs the end-to-end tests
yarn test:e2e
# Describes the test coverage
yarn test:cov
The server package has a Dockerfile which builds a lightweight (based on the alpine project) production ready Docker image.
For more information about the Docker images, see the main README.md.