|
| 1 | +# Environment Variables Example |
| 2 | + |
| 3 | +This example shows how to use environment variables with Nullstack. |
| 4 | + |
| 5 | +Start by renaming the **[.env.example](./.env.example)** file to **.env** (recommended to be kept in [**.gitignore**](./.gitignore)). |
| 6 | + |
| 7 | +Then take a look at it's contents and comments describing what every key become after Nullstack consumes it. |
| 8 | + |
| 9 | +> 💡 By default Nullstack consumes the **.env** file with exception of when a `-n`/`--name` flag is passed, e.g. `nullstack start --name=test`, in this case it searches for a **.env.test** |
| 10 | +
|
| 11 | +[Application](./src/Application.tsx) component is the main code and logs values only available at server brought from **.env**: `NULLSTACK_SERVER_PORT` and `COMMON_VARIABLE`. |
| 12 | + |
| 13 | +[KeysTable](./src/KeysTable.tsx) is a [stateless component](https://nullstack.app/stateless-components) that just illustrates how all keys from **.env** are or not available to the browser. |
| 14 | + |
| 15 | +[AccessCount](./src/AccessCount.tsx) component brings a more functional use of **.env** keys featuring a counter to API ([server functions](https://nullstack.app/server-functions)) access. |
| 16 | + |
| 17 | +- The API is only accessed passing the right password as stored at **.env**'s `NULLSTACK_SECRETS_PASSWORD`. |
| 18 | +- It was written as when running the app with the `test` script (e.g. `npm run test`) `--name=test` will be passed, making Nullstack use the **.env.test** which sets `NULLSTACK_SETTINGS_DISABLE_COUNT` (`settings.disableCount`) used to fully disable API access and count at all. |
| 19 | + |
| 20 | +> 💡🏷️ [**src/environment.d.ts**](./src/environment.d.ts) declares types for **.env** keys adding autocomplete for them at both `process.env` and context keys `settings`/`server` |
| 21 | +
|
| 22 | +## Deploy your own |
| 23 | + |
| 24 | +Deploy it now with [Vercel](https://vercel.com) or preview on [StackBlitz](https://stackblitz.com): |
| 25 | + |
| 26 | +[](https://vercel.com/new/clone?repository-url=https://github.com/GuiDevloper/nullstack-examples/tree/main/examples/environment-variables&project-name=environment-variables&repo-name=environment-variables&demo-title=Nullstack+EnvironmentVariables&demo-description=Nullstack+example+of+environment+variables&demo-url=https://github.com/GuiDevloper/nullstack-examples/tree/main/examples/environment-variables&demo-image=https://nullstack.app/image-1200x630.png) |
| 27 | +[](https://stackblitz.com/fork/github/GuiDevloper/nullstack-examples/tree/main/examples/environment-variables?title=Nullstack+EnvironmentVariables) |
| 28 | + |
| 29 | +## How to use |
| 30 | + |
| 31 | +Execute [`nulla create`](https://github.com/GuiDevloper/nulla) with [npm](https://docs.npmjs.com/cli/init): |
| 32 | + |
| 33 | +```bash |
| 34 | +npx nulla create --example environment-variables environment-variables-app |
| 35 | +``` |
| 36 | + |
| 37 | +Then deploy it with [Vercel](https://github.com/GuiDevloper/nulla/blob/main/docs/en-US/deploy-vercel.md), [Heroku](https://github.com/GuiDevloper/nulla/blob/main/docs/en-US/deploy-heroku.md) or [Netlify](https://github.com/GuiDevloper/nulla/blob/main/docs/en-US/deploy-netlify.md). |
0 commit comments