---
# System prepended metadata

title: Deploying

---

# Deploying
  
  
---
let's start with this senario ..
we are building a simple app  ok  cool .. it's working locally on our PC when we access it using our local browser at specific port and we can  aslo display our app to other people by sending a photo or a video , but they can't get access to the app and they can't try it .
that is the reason why we need to deploy the application
we want to show our applicaton to the world.
so every one can have access to our app from his PC .
in a while we will see a tutrial of how to do this ..
![](https://i.imgur.com/muvBdKF.png)

before that let's understand some concepts 


---

# what is cloud computing ?
 describes the hosting and delivery of information and on-demand computing resources on the Internet using a remote network of servers
 There are three basic service categories SaaS, PaaS and IaaS. in our case we use a Paas
 ![](https://i.imgur.com/0Gb1mJC.jpg)
 


---


# PaaS(Platform as a Service)

is a cloud computing service that gives users the ability to develop, launch, and manage apps without having to deal with the infrastructure required for building apps.
PaaS examples: AWS Elastic Beanstalk, Heroku, Windows Azure (mostly used as PaaS)



---


let's see a Demo 
in this demo i wanna show you how to deploy our node girls app that we work on it today.
let's start ...!

1) register to heroku ..it is free
2) push your code to github ... but before you push it ..ensure that you change the start script to node server.js because heroku will run a node server, and ensure that you add enviroment variable to the port address(const PORT=process.env.port || port) --- this line means : use port 4000 is it is accesible if not choose a random port 
3) now we have a pushed project on github and we have an heroku account so we can start deploying our app 
4) press on deploy button
5) and connect to github .. choose a repo that you want to deploy and shoose a master branch 
6) finally press deploy branch
7) it will build your server ..and you can see the logs if there is an error or if it success 
8) when it is done press on view button
----> hoop it's your server app running .. now i can sed you the url , so you can see it on your PC 



# Environment variables
---

# What is environment variables?
Environment variables are the best way of storing sensitive data like API Keys, Login Credentials and Database Passwords.
---
# why to use environment variables?
If you care about making your app run on any computer or cloud , then you should use them. Why? Because they externalize all environment specific aspects of your app and keep your app encapsulated. Now you can run your app anywhere by modifying the environment variables without changing your code and without rebuilding it!
A .env file is a great way to see all of your environment variables in one place.
---
# Why might some variables in your code need to change for different environments?
* Environment variables are excellent for decoupling application configurations.
* By relying on external configurations, your app can easily be deployed on different environments.
* These changes are independent of code changes, so they do not require your application to be rebuilt to     change.
* Data which changes depending on the environment your app is running on should be set as environment         variables. Some common examples are:
    • HTTP Port and Address
    • Database, cache, and other storage connection information
    • Location of static files/folders
    • Endpoints of external services
        ◦ For example, on a development environment your app will point to a test API URL, whereas in a               production environment your app will point to the live API URL.
---        
# Why is it a bad idea to include those variables in a public repository?
Sensitive data like API keys should not be in the source code, or known to persons who do not need access to those external services.
Storing your environment variables in a file comes with one golden rule:
***never commit it to the source code repository. You do not want outsiders gaining access to secrets, like API keys*** 
---
# What modules might you use to help manage environment variables?
***dotenv*** is a zero-dependency module that loads environment variables from a .env file into process.env. Storing configuration in the environment separate from code is based on The Twelve-Factor App methodology.
This library does one simple task: loads environment variables from a .env file into the process.env variable in Node.js.
 ***npm install dotenv***
---
# Conclusion Of Environment variables
* Environment variables exist outside our application's code.
* they are available where our application is running.
* They can be used to decouple our application's configuration from its code, which allows our apps to be     easily deployed across different environments.
* With Node.js apps, environment variables are available through the process.env global variable.
* We can use the dotenv library which allows us to define our environment variables in a .envfile.
* The .env file should never be in the source code repository.

---
