# Sample Web App

*This is the same sample web app deployed on* [*https://sample.techup.live*](https://sample.techup.live)

{% hint style="info" %}
**In this exercise, we will teach you how to:**&#x20;

1. Create a GitHub repository to store your sample code (in Git files)
2. Upload ("commit and push") your HTML + CSS + JS code to GitHub
3. Connect a sample website to a database on Postgres

**This exercise is meant to simulate how you would deploy your live website onto the Internet.**&#x20;
{% endhint %}

## Web technologies powering the Sample Web App

The Sample Web App is hosted on Render.com, a cloud-based application hosting platform with the following components:

* **Frontend:** HTML, CSS and Javascript
* **Backend:** NodeJS, Express, EJS
* **Database:** Postgres

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FibNvDLD3PiGgh30g3cPs%2FScreenshot%202024-04-21%20at%207.53.10%E2%80%AFPM.png?alt=media&#x26;token=bf135402-de6a-46a9-82c6-65f92abadaa5" alt=""><figcaption><p>Extremely simple System architecture of Sample Web App</p></figcaption></figure>

## Sign in to GitHub and Render

You will need to be signed in to GitHub and Render.com before you setup the Sample Web Application. For instructions on how to sign up for a GitHub and Render account respectively, please refer to the following pages.

{% hint style="info" %}
**Note:** You will need to create a GitHub account first before creating a Render account. You will be using the "Sign up with GitHub" option when creating an account on Render.
{% endhint %}

{% content-ref url="../admin-instructions/tooling-and-software/github" %}
[github](https://info.techup.live/admin-instructions/tooling-and-software/github)
{% endcontent-ref %}

{% content-ref url="../admin-instructions/tooling-and-software/render" %}
[render](https://info.techup.live/admin-instructions/tooling-and-software/render)
{% endcontent-ref %}

## Instructions on setting up your sample web app

{% hint style="info" %}
If you prefer to watch a video of this setup you may watch it [here](https://drive.google.com/file/d/18UTrfh8eo3emeDxEZ32Kig6N2qdvDi-c/view?usp=sharing) instead.
{% endhint %}

### 1. Create a new GitHub Repository

* Go to <https://github.com/new> to create a new repository
* Type in your own repository name (this could be any name)
* The description for your repository can be empty
* Your repository should by default be Public
* Ensure that you check the "**Add a README file**"
* Ensure that you select **Node** as the .gitignore template
* The license can be at it's default setting of None
* Note that your default branch may be called "master" or "main", this is okay and you can leave it (mine is currently set as "develop").
* Click on "Create repository" to create your new GitHub Repository

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2F8LWt3cGDOy78ANQO8DxP%2FScreenshot%202024-04-02%20at%207.13.05%E2%80%AFPM.png?alt=media&#x26;token=bfffcb3b-2bdc-40f0-b429-7320f64c15aa" alt=""><figcaption><p>A sample of the options selected before clicking "Create repository"</p></figcaption></figure>

### 2. Open your newly created repository in Codespaces

* You should have been re-directed to your newly created repository
* Click on the green "Code" button, select the "Codespaces" tab and click **"Create Codespace on develop"**. (Note: your branch may be called "master" or "main" and this is fine)

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FwY3VnFBHnDJmEEcPPd9n%2FScreenshot%202024-04-02%20at%207.25.55%E2%80%AFPM.png?alt=media&#x26;token=81909808-efdf-470d-9130-21d56e0c1017" alt=""><figcaption><p>Click on create codespace to open up codespaces for your repository</p></figcaption></figure>

### 3. Download the source code and unzip the file

* Your GitHub Codespace should have opened up in a new tab after creating a codespace in the previous step.

### Click on the link below to download the source code for the sample app here and unzip the file:&#x20;

{% file src="<https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FDY7vZj19Se6WzeuxqlcU%2Ftechup-sample-blog-9apr.zip?alt=media&token=94f07f74-83f5-4156-9de0-1f5b20f883a8>" %}

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FMusLTMaF6SXVYGA2yP0Y%2FScreenshot%202024-04-02%20at%207.36.57%E2%80%AFPM.png?alt=media&#x26;token=f526a906-b61d-429c-9fb1-bbc7aec3210e" alt=""><figcaption><p>You should see the following files and folders after unzipping the zip file  </p></figcaption></figure>

### 4. Drag and drop sample app code into Codespaces

* After completing Step 2, a new tab should have opened with your Codespace similar to the image shown below.

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FuwFb3a8IQRDRdYWxwXa3%2FScreenshot%202024-04-02%20at%207.39.37%E2%80%AFPM.png?alt=media&#x26;token=5246f71b-106f-4932-bf5e-3185efa4484e" alt=""><figcaption><p>Your Codespace for your newly created GitHub repository</p></figcaption></figure>

* Drag and drop all files and folders in Step 3 into the sidebar of your Codespace. IMPORTANT: Ensure that you select all the files and folders INSIDE the unzipped file, do not upload the whole zip file (see the below images if you are unsure).

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FTW03d72ZrmcIDPNV3KJK%2FAnnotated%20Screenshot%202024-04-02%20at%207.36.57%E2%80%AFPM.png?alt=media&#x26;token=4f9f82c1-9942-42e7-93a2-a738bfa2ad12" alt=""><figcaption><p>Select all the files and folders INSIDE the unzipped file to upload to Codespace</p></figcaption></figure>

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2Fe0iYMGa00Rnv4V1i8KnK%2FAnnotated%20Screenshot%202024-04-02%20at%207.39.37%E2%80%AFPM.png?alt=media&#x26;token=2c0c0f1c-53f6-4465-9c92-ba90c93ecb71" alt=""><figcaption><p>Drop the selected folders and files in the section marked by the red box</p></figcaption></figure>

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2Fg8LS2fmQWHZKYeJI8of7%2F0b1aaa01-4d38-43c7-be41-3c52eb00f67e.gif?alt=media&#x26;token=cce2ceb8-beaf-49f7-8775-4dbb37018611" alt=""><figcaption><p>Ensure you drag and drop the correct files in the right location</p></figcaption></figure>

* This uploads the sample web app source code to your Codespace
* After dragging and dropping, your Codespace should look like this with the following files in the sidebar

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FdIsVcVLec1p6egrwz9PM%2FScreenshot%202024-04-02%20at%207.42.28%E2%80%AFPM.png?alt=media&#x26;token=fe34d8b1-b0c1-4768-83d9-490f725881ce" alt=""><figcaption><p>Note the newly added folders of "prisma", "public", "views" and files "package-lock.json", "package.json", "server.js"</p></figcaption></figure>

### 5. Commit & Push to your repository

* Click on the Source Control icon (the icon which has blue numbers on it, in the image above it's the third tab on the left)
* Enter a commit message of "Uploaded sample code"
* Click on the dropdown next to "Commit" and click on "Commit & Push".
* Note that you will have to click on the drop down arrow for "Commit & Push" to show up

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2Fm3JS0he3FQQs12UlMNiW%2FScreenshot%202024-04-02%20at%208.00.42%E2%80%AFPM.png?alt=media&#x26;token=3fca2d72-ad45-4940-865a-22091fe0dee2" alt=""><figcaption><p>Click on the Source Control tab, write a commit message and click on "Commit &#x26; Push" from the drop down list</p></figcaption></figure>

* If the following pop-up appears, select "Always"

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2F7efl5L802iwo4eRROmXG%2FScreenshot%202024-04-02%20at%208.03.59%E2%80%AFPM.png?alt=media&#x26;token=53948823-4fae-496c-9692-039fb1e0f375" alt=""><figcaption><p>Select "Always" should this pop-up appear after clicking "Commit &#x26; Push"</p></figcaption></figure>

* Another pop up may appear at the bottom right of your screen, select the option of  "Yes"

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FFjTEj4e5yHUFjLwS3qzj%2FScreenshot%202024-04-02%20at%208.04.11%E2%80%AFPM.png?alt=media&#x26;token=8270c77b-9954-41ae-8850-a84a2a7881be" alt=""><figcaption><p>Select "Yes" should this pop-up appear on the bottom right of your screen</p></figcaption></figure>

* Check that your files are in your GitHub repository by going to <https://github.com> and selecting the repository you created in Step 1

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FYKx8WLqt8HHJaV2iKQXF%2FScreenshot%202024-04-21%20at%207.37.47%E2%80%AFPM.png?alt=media&#x26;token=15183ee8-94b5-445e-a24c-52b32df58347" alt=""><figcaption><p>Select your repository in the sidebar on the left</p></figcaption></figure>

* You should now see that there are the newly uploaded files in this repository

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FkbrWLMi1xW1nyGZMNfAc%2FScreenshot%202024-04-21%20at%207.41.39%E2%80%AFPM.png?alt=media&#x26;token=6af84110-8526-4a68-883c-f07358e9aeb3" alt=""><figcaption><p>The files should now be in your repository you created</p></figcaption></figure>

### 6. Create a new PostgreSQL database on Render

* Go to <https://dashboard.render.com/new/database> to create a new PostgreSQL database on Render.
* Type in a Name for your PostgreSQL instance (I've named mine techup-sample-database)
* Leave the Database field blank
* Leave the User field blank
* Change the Region to "Singapore (Southeast Asia)
* Leave PostgreSQL Version as the default value of 16
* Leave the Datadog API Key field blank
* Change your instance type to FREE
* Click on "Create Database"

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FNC5dkGr5KJhNWpLEzrvX%2FScreenshot%202024-04-02%20at%208.16.22%E2%80%AFPM.png?alt=media&#x26;token=c5345357-28ff-4a2e-95d0-5c58cb9f0557" alt=""><figcaption><p>Enter a Name for your instance and change the Region to Singapore while leaving other fields as their default values</p></figcaption></figure>

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FXcXTjQpT3VvTmJq2jLku%2FScreenshot%202024-04-02%20at%208.16.26%E2%80%AFPM.png?alt=media&#x26;token=9f2d739f-6041-4bf0-b23f-3c3f1fd933d1" alt=""><figcaption><p>Change your Instance Type to FREE before clicking on Create Database</p></figcaption></figure>

* Your database should now be created (after about 30 seconds)
* Scroll down to the section on Connections and take note of the `External Database URL`
* Copy this `External Database URL` to a blank word document (or anywhere else such as Notepad) as you will be using it later to add it as an environment variable.
* <mark style="background-color:red;">**IMPORTANT: PLS ENSURE YOU HAVE COPIED THIS URL AS YOU NEED IT LATER.**</mark>

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2Fae3RuJBJes17DBR9Efr6%2FScreenshot%202024-04-02%20at%208.20.52%E2%80%AFPM.png?alt=media&#x26;token=83cce57a-2660-410e-8cac-e3f78ba2ae93" alt=""><figcaption><p>Click on "Copy to clipboard" for the External Database URL to copy this URL for use later</p></figcaption></figure>

### 7. Create a new Web Service on Render

* Go to <https://dashboard.render.com/create?type=web> to create a new Web Service (alternatively you may also click on the purple New + button at the top of the page and select Web Service)
* Ensure "Build and deploy from a Git repository" is selected then click Next

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FY4bkU3e2kVlEonk4r5BW%2FScreenshot%202024-04-02%20at%208.24.19%E2%80%AFPM.png?alt=media&#x26;token=bee62f78-0b67-4c23-93df-4645453a2052" alt=""><figcaption><p>Ensure the first option of "Build and deploy from a Git repository" is selected</p></figcaption></figure>

* On the right column you will need to click on "+ Connect Account" to connect your GitHub account to Render.
* Click on "Connect" for the repository name which your created in Step 1

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FJERevu5IoEkzbbUXHAqO%2FScreenshot%202024-04-02%20at%208.26.12%E2%80%AFPM.png?alt=media&#x26;token=56c6ad36-e1cd-49f5-8e24-c932688db6a9" alt=""><figcaption><p>Click Connect on the repository created in Step 1 (if this dropdown list is not showing you will need to click on + Connect Account on the page to connect your GitHub account to Render)</p></figcaption></figure>

* Leave the Name field as it's default value
* Ensure that the Region is set to "Singapore (Southeast Asia)"
* Ensure that your Branch is selected (yours may be called "master" or "main")
* Leave the Root Directory field blank
* Leave the Runtime as "Node"
* Change the Build Command to `yarn; npx prisma db push; npx prisma generate`&#x20;

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FwzQCipXg56Vm90sJdGsq%2FScreenshot%202024-04-02%20at%208.35.12%E2%80%AFPM.png?alt=media&#x26;token=bf0580a6-bcc3-40ab-a4eb-1a9dad2cf096" alt=""><figcaption><p>Change the Build Command to as shown above</p></figcaption></figure>

* Add the Start Command of `node server.js`&#x20;
* Change the Instance Type to FREE

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FasWTprs2E4LoYiaGbTrs%2FScreenshot%202024-04-02%20at%208.39.36%E2%80%AFPM.png?alt=media&#x26;token=2ee2fed9-4964-4068-9dc1-c4bc1b2a7c43" alt=""><figcaption><p>Add the Start Command to "node server.js" and change the Instance Type to FREE</p></figcaption></figure>

* Add a new Environment Variable of name `DATABASE_URL` with the value of your copied `External Database URL` in Step 6. *(If you have misplaced this URL, not to worry. Go to* [*https://dashboard.render.com/*](https://dashboard.render.com/) *and click on your database. Scroll down and simply copy the External Database URL again.)*

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FDdSEuoGLSS50uZvgbJQV%2FScreenshot%202024-04-02%20at%208.42.26%E2%80%AFPM.png?alt=media&#x26;token=b295b7ad-1d79-475f-b088-90ccfe941d73" alt=""><figcaption><p>Create a new Environment Variable of "DATABASE_URL" and paste the External Database URL copied earlier as its value</p></figcaption></figure>

* Click on the "Create Web Service" button to start deploying
* You will be redirected to a new page which shows the logs of the server build happening. It would take approximately 2-4mins for the the server to full deploy your app.
* Your app is deployed once you see the line "==> Your service is live 🎉" in the logs.

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FGjikxRJ8YNqtvFZGcUBf%2FScreenshot%202024-04-02%20at%208.45.35%E2%80%AFPM.png?alt=media&#x26;token=8c43f07a-9996-4d6b-8aea-0d8ee270c93d" alt=""><figcaption><p>Your app is ready once you see "Your service is live 🎉"</p></figcaption></figure>

### 8. Accessing your newly deployed web application

* At the top of the same deploy page with the logs, you will see a URL (in my case it is <https://techup-blog.onrender.com>). Click on this link and it will bring you to your web application.

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FVrRvaLwE0qyL6Y0KFuRw%2FScreenshot%202024-04-02%20at%208.49.14%E2%80%AFPM.png?alt=media&#x26;token=06865914-bbf4-4f77-bf5f-7b05fe80edea" alt=""><figcaption><p>Click on the URL at the top left to bring you to your newly deployed web app</p></figcaption></figure>

* You should now be able to view the sample web app which you have deployed on your own Render server!

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FVfZvkVjfnnENXLXGuYjR%2FScreenshot%202024-04-02%20at%208.51.29%E2%80%AFPM.png?alt=media&#x26;token=c93ec5c1-09f6-4a1c-b39a-bfc1c7c52b26" alt=""><figcaption><p>Congrats!! You have successfully deployed the sample web app on your own Render server</p></figcaption></figure>

* Have fun playing around with the sample web app by Adding and Deleting blog posts!

## Optional: How do I view what is inside my Postgres database which I created on Render.com?

### 1. Download and install TablePlus from <https://tableplus.com/>

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2Faf7nyrPU2fXTpuporfeu%2FScreenshot%202024-04-21%20at%208.41.42%E2%80%AFPM.png?alt=media&#x26;token=f1c8ba51-02a4-474c-b483-77a9235269e7" alt=""><figcaption><p>Dowload TablePlus for Mac or TablePlus for Windows depedning on what laptop you have</p></figcaption></figure>

### 2. Open TablePlus and click on the + icon to add a new connection

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FgnncFnNmRmEjaCkOLgy0%2FScreenshot%202024-04-21%20at%208.41.26%E2%80%AFPM.png?alt=media&#x26;token=4fbca980-e7a1-406c-b73f-e6a9d84d5441" alt=""><figcaption><p>Click on the + icon to add a new connection</p></figcaption></figure>

### 3. Click on Import from URL

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FYGNjWh6qYLtHtxFSwtVs%2FScreenshot%202024-04-21%20at%208.53.38%E2%80%AFPM.png?alt=media&#x26;token=5463c166-7fe8-47a9-99f2-753950c251c7" alt=""><figcaption><p>Click on Import from URL</p></figcaption></figure>

### 4. Paste the `External Database URL` (which you copied earlier when setting up Postgres) into the text field then click Import

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FyKOmDKL0QidkZeLedt5F%2FScreenshot%202024-04-21%20at%209.25.10%E2%80%AFPM.png?alt=media&#x26;token=dbe7baa5-1693-4222-810e-b64d47a84d8b" alt=""><figcaption><p>Paste your External URL into the field then click Import</p></figcaption></figure>

### 5. Click on Connect (you may rename the "Name" field or leave it as it's default)

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FbxuqkzR9FsFr61B9hf77%2FScreenshot%202024-04-21%20at%209.28.13%E2%80%AFPM.png?alt=media&#x26;token=8a251981-70b9-45f5-959e-8372c2e0034d" alt=""><figcaption><p>Click on connect</p></figcaption></figure>

### 6. You have now successfully connected to the database from TablePlus and you may click on the Post table in the left sidebar to view the contents

<figure><img src="https://4000871720-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JeKot5J5CqiKNQnsgZV%2Fuploads%2FwbgoWTkifV4BtSwe2aLK%2FScreenshot%202024-04-21%20at%209.31.57%E2%80%AFPM.png?alt=media&#x26;token=f33b99c5-667a-412b-93f6-c0895efd0267" alt=""><figcaption><p>Success! You can click on the Post table to view the contents</p></figcaption></figure>
