To set up and run the project, follow the instructions below:
Create a .env
file in the root of your project directory and add the following environment variables:
DATABASE_USER_NAME=<your_mongo_db_username>
DATABASE_PASSWORD=<your_mongo_db_password>
DATABASE_NAME=<your_mongo_db_name>
JWT_SECRET=<your_jwt_secret>
Replace the following placeholders with actual values:
<your_mongo_db_username>
: The MongoDB username.<your_mongo_db_password>
: The MongoDB password.<your_mongo_db_name>
: The name of your MongoDB database.<your_jwt_secret>
: The jwt secrete you can Generate from https://jwtsecret.com/generate
-
Create a MongoDB Atlas account:
- Visit MongoDB Atlas and create a new account if you don't already have one.
-
Create a Cluster:
- After logging in, click on "Build a Cluster" and choose the free-tier cluster (M0).
- Follow the prompts to select your cloud provider and region.
-
Create a Database User:
- Go to the "Database Access" section in the left sidebar.
- Click on "Add New Database User."
- Set a username and password for your MongoDB user. You will use these credentials in the
.env
file.
-
Whitelist Your IP Address:
- Go to the "Network Access" section in the left sidebar.
- Click on "Add IP Address" and allow access from anywhere (or just your IP).
-
Get the Connection String:
- Go to the "Clusters" section.
- Click on "Connect" and then "Connect your application."
- Copy the connection string provided and replace
<your_mongo_db_username>
,<your_mongo_db_password>
and<your_mongo_db_name>
in the.env
file accordingly.
Your .env
file should now have the credentials for your MongoDB Atlas cluster.
const dbUsername = encodeURIComponent(process.env.DATABASE_USER_NAME);
const dbPassword = encodeURIComponent(process.env.DATABASE_PASSWORD);
const dbName = encodeURIComponent(process.env.DATABASE_NAME);
// The connection string using the credentials
const mongoURI = `mongodb+srv://${dbUsername}:${dbPassword}@${dbName}.mongodb.net/?retryWrites=true&w=majority`;
-
Clone the repository: If you haven't cloned the repository yet, do so with:
git clone <repository_url> cd <repository_name>
-
Install Node.js Dependencies: Inside the project directory, run:
npm install
-
Download Docker Desktop:
- Go to Docker Desktop for Windows and download the installer.
- Run the installer and follow the instructions to install Docker.
- After installation, restart your system if prompted.
-
Run Docker Desktop:
- Launch Docker Desktop and ensure it is running properly.
-
Check Docker Installation: Open a terminal (e.g., Command Prompt or PowerShell) and run the following command to check if Docker is installed correctly:
docker --version
-
Update Package Index:
sudo apt-get update
-
Install Required Packages:
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
-
Add Docker’s Official GPG Key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
-
Set Up the Stable Docker Repository:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
-
Install Docker:
sudo apt-get update sudo apt-get install docker-ce
-
Verify Installation:
docker --version
-
Run Docker Without
sudo
(Optional):To run Docker commands without
sudo
, add your user to the Docker group:sudo usermod -aG docker $USER
Log out and log back in for changes to take effect.
-
Ensure
docker-compose.yml
exists in your project directory. If not, create one to define your services (like MongoDB or the application itself). -
Start Docker Compose:
With Docker and Docker Compose installed, run the following command to start your containers:
docker-compose up --build
This will build your app container and start MongoDB.
-
Access the Application: Once the containers are up, your application should be accessible at
http://localhost:3000
in your browser.
If you prefer to run the app locally without Docker, you can run:
-
Start the Server:
npm run dev
-
Access the Application: Open your browser and go to
http://localhost:3000
.
That's it! You have successfully set up your environment and can start working with the API.