Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment variables - CS2 #105

Open
ezspot opened this issue Nov 18, 2024 · 1 comment
Open

Environment variables - CS2 #105

ezspot opened this issue Nov 18, 2024 · 1 comment

Comments

@ezspot
Copy link

ezspot commented Nov 18, 2024

Hi, Is there a place where i can see what variables are possible to use?

@Innocent6303
Copy link

Setup .env File

To set up your .env file with the necessary environment variables, follow these steps:

1. Create a .env file

In the root folder of your project (either frontend or backend), create a .env file.

  • For Backend: Place the .env file in the root directory of the backend folder.
  • For Frontend: Place the .env file in the root directory of the frontend folder.

2. Add Environment Variables

Open the .env file and define the environment variables as shown below:

DB_HOST=localhost:3000  # If you're running MongoDB locally, use localhost and port, or use Docker container name if running in Docker
DB_PORT=27017           # Default port for MongoDB, you can change this if needed
DB_NAME=wishlist_db     # The name of your MongoDB database, can be customized (e.g., alexandra)
SECRET_KEY=my_secret_key  # For development, any string is fine, but make sure it is strong and unique in production

Environment Variables Setup

You can configure the necessary environment variables in your .env file as follows:

Required Environment Variables

  • DB_HOST:
    If you're working locally, set this to localhost:3000. If you're using Docker, replace this with the container name for MongoDB.

  • DB_PORT:
    MongoDB runs on port 27017 by default, but you can change it if necessary.

  • DB_NAME:
    This is the name of the database you're using. You can change wishlist_db to any name you prefer (e.g., alexandra).

  • SECRET_KEY:
    For development, you can use any string, but make sure to use a unique, strong secret key for production.

Example .env File:

Create a .env file in your project root directory and add the following lines:

DB_HOST=localhost:3000  # If you're working locally, set this to localhost:3000. 
                         # If you're using Docker, replace this with the container name for MongoDB.

DB_PORT=8000          # MongoDB runs on port 27017 by default, but you can change it if necessary.

DB_NAME=alexandra     # This is the name of the database you're using. 

SECRET_KEY=sjflskienn88ka # For development, you can use any string, but make sure to use a unique, strong secret key for production.

Using the Environment Variables

Once you've set up your .env file, you can use the environment variables in your project. For type-safe usage, you can use the ts-typesafe-env package to manage the environment variables with type safety.

install ts-typesafe-env

npm install ts-typesafe-env

Access Environment Variables in Your Code

Once installed, you can import and use the environment variables as shown below:

import { TypeSafeEnv} from 'ts-typesafe-env';

const dbHost = TypeSafeEnv.getString('DB_HOST');
const dbPort = TypeSafeEnv.getNumber('DB_PORT');
const dbName = TypeSafeEnv.getString('DB_NAME');
const secretKey = TypeSafeEnv.getString('SECRET_KEY');

console.log(`Connecting to MongoDB at ${dbHost}:${dbPort}/${dbName}`);

This ensure that your environment variables are loaded safely and with type-checking

Let me know if you need further assistance! 😊

If you have any doubts, you can visit the GitHub repository of typesafe-env for more details. Click me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants