Env vars in main file
Using environment variables in the main file.
If you notice, in the main file, the value used for the app port is obtained from the .env and, if not found, is defaulted to 3000. In here, we cannot use a configuration namespace because the application is still being initialized, but we can use the ConfigService
to access the env.
First, let's create, at the top of the .env file, the variable for the port. Remember to also add it in the .env.example file.
After that, we should also validate it in the validation schema, in the env.constants file. It can use the same validation as the one for the database port.
Then, in the main file, we can get a reference to the ConfigService
from the app
, and then obtain the port value and use it.
Commit - Using environment variables in main file
Last updated