Skip to main content

Environment Validator

The environment validator provider is a provider that ExpressoTS offers out-of-the-box. It is used to validate the existence of the .env file and its content. It helps the developer to avoid runtime errors caused by missing environment variables.

tip

It is only included in the opinionated template or when you create a class that extends the Application class.

You can find it in the src/providers/application folder in the opinionated template.

The application.provider is the main provider of the application that is responsible to control the Application Lifecycle.

Inside of the Application class you can find three methods that are called in the following order:

  • configureServices() : Configure services that should be initialized before the server starts.
  • postServerInitialization() : Configure services that should be executed after the server starts.
  • serverShutdown() : Perform actions or cleanup after the server is shutdown.

Environment validator provider is called in the configureServices() method preventing the application from starting if the .env file is missing or if it has missing environment variables.

protected configureServices(): void {
Environments.checkAll();
}

The environment validator provider also has a method called get() that returns the value of an environment variable. It is used to get a specific environment variable value passing its name as a parameter.

Environments.get("PORT");

Support the Project

ExpressoTS is an MIT-licensed open source project. It's an independent project with ongoing development made possible thanks to your support. If you'd like to help, please consider: