# Configuration
# Environment variables
When building an application, you may want different configurations based on the environment your code is running in. To fulfill this requirement, ExpressWebJs uses the dotenv library.
Inside the root of every new ExpressWebJs project, you’ll find .example.env file. You can create your .env file and set up your environment configuration. To do that on terminal, run:
cp example.env .env
Many important configuration values are defined using the .env file that exists at the root of your application. Your .env file should not be committed to your application's source control, since each developer / server using your application could require a different environment configuration.
Furthermore, this would be a security risk in the event an intruder gains access to your source control repository, since any sensitive credentials would get exposed.
Expresswebjs helps you have a maintainable codebase by providing a dedicated location for storing application configuration which is the config directory.
You can access configuration values in the App/Config directory.
# Acessing your environment variable
ExpressWebJs gives you a nice provider which is env provider to help access your environment variables.
import { env } from "Elucidate/ENV";
env.get("APP_KEY");
// With the second parameter as the default values
env.get("APP_HOST", "0.0.0.0");
env.get("APP_PORT", 5100);
You can still make use of Node.js native syntax.
process.env.APP_KEY;
process.env.APP_HOST;
process.env.APP_PORT;
# Maintenance Mode
You can switch your application to maintenance mode in Config/app.ts file in maintenanceMode section. When your application is in maintenance mode, a default message "Application is in maintenance mode" will be sent for all requests accessing your application. This makes it easy to "disable" your application while it is updating or when you are performing maintenance. A maintenance mode check is included in the default middleware stack for your application. If the application is in maintenance mode, a MaintenanceModeException will be thrown with a status code of 503.
To enable maintenance mode, change the mode in maintenanceMode section to true:
/*
|--------------------------------------------------------------------------
| Application maintenance mode
|--------------------------------------------------------------------------
| Mode ==> Determine if application is in maintenance mode or not. Default value is false
| Message ==> The message to render while in maintenance mode.
| Retry ==> The number of seconds after which the request may be retried
| Endpoints ==> The endPoints that should be reachable while maintenance mode is enabled.
| endpoints example: ['api/users','api/accounts']
|
*/
maintenanceMode: {
mode: true, //enable and disable maintenance mode
message: "Application is in maintenance mode.",
retry: 50,
endPoints: [],
},
You can also specify the endpoints you want to be accessable while your application is in maintenaince mode. Let's make 'api/users' and 'api/accounts' to be accessable like so:
maintenanceMode: {
mode: true, //enable and disable maintenance mode
message: "Application is in maintenance mode.",
retry: 50,
endPoints: ['api/users','api/accounts'],
},