Tech Junkie Blog - Real World Tutorials, Happy Coding!: October 2021

Friday, October 29, 2021

In JavaScript anything defined outside of a function is considered a global scope while anything that's defined inside a function is called a variable scope (local or function scope) meaning it exists inside the lifetime of the function.  Let's use code to demonstrate this point


var streetFighter = "Ryu";

function displayFighter()
{
    var streetFighter = "Ken";

    console.log(streetFighter);
}

displayFighter();


Thursday, October 28, 2021

In JavaScript there are two ways to access an object, first by dot notation like this

product.name

or with array notation

product["name"]

They both get the job done.   However with the array syntax [""] you access the object as if it's an array but instead of accessing it by numbers or index you access it by the property names.  That's because JavaScript objects are associative arrays.  They look like other objects in other languages such as C, C++, and Java.  However they behave very differently.

In other languages objects are defined in advance, their properties are methods are fixed.  To add a property or method to the object you have to change the class the object instance is created from.

Wednesday, October 27, 2021

 In this post we are going to set up our website to serve up https traffic so that our traffic can be encrypted.  In this post the first part of the series we are going to request a certificate from the Certificate Manager in AWS.

1. The first thing we need to do is create a certificate, In the AWS search field search for Certificate Manager then click on the drop down auto complete choice.



2. Click on "Get started" under "Provision certificates"

Tuesday, October 26, 2021

In a typical git scenario you would have a new branch for developers to work on, the when he or she is done. You do your code reviews and what not.  After you are satisfied with the results you would want to merge the new branch into the master branch.

In this post I am going to show you how to merge an existing branch into a master branch

Here are the steps:

1. First you want to checkout the master branch to work on it with the command git checkout master












Monday, October 25, 2021

Now that we've learned to create objects in all sorts of ways it's time to set and get the object properties that we've created.  In this blog we will go over how to access the object properties either to set them or get them.

First let's create an object that we can work with:

Type in the following code in the between the <script type="text/javascript"></script> tag in an html page.


        var product = new Object();

        product.name = "Chai";
        product.category = "Tea";
        product.country = "India";
        product.supplier = {
            name: "ACME Tea Of India",
            location: "New Delhi"
        };

        console.log(product);
If you run the page with the code above you will see the following in the browser console:

Friday, October 22, 2021

When the browser window opens and navigates to a webpage with your JavaScript code. A global object is created for your JavaScript program.

If you type the following line as the first line of code in your JavaScript file

var globalObj = this;

What do you think will get?  In this case you are referencing the Window object, which is JavaScript's object for the browser window.  As you can see there's a whole bunch of global properties and methods already defined at the global level.

Thursday, October 21, 2021

There are times when you will get an error when you tried to access a property in an object, sometimes you think a property exists in the object but it does not.  Let's use the product object from the previous blog post as an example.


        var product = new Object();

        product.name = "Chai";
        product.category = "Tea";
        product.country = "India";
        product.supplier = {
            name: "ACME Tea Of India",
            location: "New Delhi"
        };

Let's say another developer works on the project and he assumes that since there's a "country" property that there should be a "city" property.  If he tries to access the access the "city" property in the product object he will get an undefined, because the property does not exist.   If he types the following he will get the undefined message.

Wednesday, October 20, 2021

 In this pose we are going to implement auto scaling on our instances.  Auto scaling is a feature on AWS that automatically scaled horizontally either based on metrics or the health of an instance. In this post we are going to setup auto scaling on an Application Load Balancer.  

1. The first thing we have to do is setup an Auto Scaling Group under "Auto Scaling" click on "Auto Scaling Groups"

2. Click on the "Create Auto Scaling group" button

3. Give your auto scaling group a name, then click on the "Create a launch template"


Tuesday, October 19, 2021

In previous posts we created a new git repository locally and on github and then sync them up.  In this post we are going to create a new branch in the local git repository and push it to the remote repository.


First let's create a new branch with the following command in the local git repository

git checkout -b "Hour2"








Now following the tutorials in this post Hour 2: Enable ASP.NET Core to Serve Static Files

After you are done open the command line in the folder and type git add .

Monday, October 18, 2021

In JavaScript you can delete an object's property with the delete operator.  Let's use the product object again as an example.

Let say you have the following JavaScript code to create a product object.


        var product = new Object();

        product.name = "Chai";
        product.category = "Tea";
        product.country = "India";
        product.badProperty = "Bad Property";
        product.badProperty2 = "Bad Property 2";
        product.supplier = {
            name: "ACME Tea Of India",
            location: "New Delhi"
        };

console.log(product);

Friday, October 15, 2021

One of the most common task you have to do as a Linux administrator is to add a new user.  Especially developers who always wants root access. Docker needs root access, however the person who is administering Docker is probably not the system administrator.  Most likely it will be the application developer. To accomplish this task you can use the useradd command in the Terminal session then add the new user to the Docker group.  Follow the steps below to add a new user to CentOS.

Thursday, October 14, 2021

Most people don't realize JavaScript has object property setters and getters or accessor methods.  When an object has a setter only it is a write only property, when it has a getter only method that it is a read only property.  If a property has both then it is a read/write property.  A perfect example is if you are working with a private property in JavaScript which has the $ prefix in front of it.

Let's say you have an object with a private property $n like the example below:


        var game = {

            $n: "Awesome Game!",

            get name() { return this.$n}
        };

The code above only has a getter name that exposes the private property $n when you console.log you should get value of the $n like the code snippet below

Wednesday, October 13, 2021

 In the previous post we went over how to create a Application Load Balancer, based on routes in the URL.  In this post we are going to change the rules so that it directs traffic based on host names.  For example we could have acmebanking.com route to target group 1 and accounts.acmebanking.com route to target group 2.  It's the same concept as the previous setup but instead of routes, we are using hostnames instead.

In order to implement this solution we need to setup a record in Route 53, which is AWS domain registration service.  You can follow along with the DNS names, but if you have a hostname registered with AWS that's even better.

1. So the first thing you want to do is go to Route 53 dashboard, go to Hosted Zones

2. Click on the hosted domain


Tuesday, October 12, 2021

In the previous post we created a local git repository, in this post we are going to create a remote repository in github and link it to the local repository that we created.

Here are the steps to create a new repository in github:

1.  Log into your github account and click on the "New" button next to the text "Repositories"










Monday, October 11, 2021

One of the main concept of working with Bootstrap is the Grid system.  It could be intimidating at first to work with the Grid system.  But the system is quite elegant and simple solution.

The most important thing you have to remember about the grid system is that it divides the page into 12 columns, and it is responsive meaning it will adjust to the size of the client's screen.

You can control the size of the column with the .col-sm, .col-md, .col-lg, .col-xl, which translates to small, medium, large, and extra large screens.  In addition to the screen size you can also control the with of the column with the -n at the end of class attribute.  For example if you want a column for a small screen to span three columns it would be .col-sm-3.

Here is the cheat sheet, I am going to use color coding for the different sizes.

Small - Green
Medium - Orange
Large - Red
Extra Large - Blue


Friday, October 8, 2021

In our previous blog posts we ran containers with the Fedora and CentOS images.  In this blog we are going to run a command to see which containers are running in our host system.  To get a list of all the containers running on our host Ubuntu system we type in the command docker ps -a command.

Thursday, October 7, 2021

The while loop statement in JavaScript simply executes a statement block until a condition is not true anymore the syntax for the while loop is as follow

while (expression is true)
{
     execute these statements
}

The while loop is an iterative loop if the condition is true and the statements are executed, it starts at the top of the loop again and executes until the expression is false.  Therefore, there's a potential for an infinite loop.

Wednesday, October 6, 2021

 In the previous post we went over how to create a Network Load Balancer, in this post we are going to create one of types of load balancer AWS offers.  We are going to create a Application Load Balancer, this balancer  is designed to work best with the typical line of business web applications.  It deals mostly with the requests/response scenarios on the web, therefore it supports the HTTP, and HTTPS protocols exclusively.  It can be setup to respond to the routes that configured or the hosts.  It all depends on how your web applications serves the client.  In a way it's the easiest load balancer type to understand because it deals with headers, URLs,  routes,  parameters, query strings and etc.

Before we create the load balancer we need to create more than one instances with a web server because we need to test that the load balancer is able to switch.

1. Create four instances with the user data to create Apache Web Servers with these commands in the User Data for instance, if you need the full instruction on how to create instances with User Data you can read this post . 

#cloud-boothook
#!/bin/bash
#Use this for your user data (script without newlines)
# install httpd (Linux 2 version)

yum update -y 
yum install -y httpd.x86_64 
systemctl start httpd.service 
systemctl enable httpd.service 
echo "Hello world from $(hostname -f)" > /var/www/html/index.html
cd /var/www/html/
cp index.html contacts.html

We just created an index.html file to write out the hostname for testing later on, we also created a new file called contacts.html so that we can have different routes.

To create more than one instance at a time, type in the number of instances in the "Number of Instances" field.  Select no preferences for the subnets






Tuesday, October 5, 2021

In this blog post we will create a Git & Github repository for the Northwind Cafe application that we've been working on.

First let's create a git local repository

1. Follow the steps on this blog post to create an empty NorthindCafe in git repository using Visual Studio Hour 1: Create ASP.NET Core Project From Scratch




























Monday, October 4, 2021

The do/while loop is similar to the while loop.  The difference is that the first the loop expression is tested after the do statement block is executed at least once.  For example let's use our countdown example again.

If we have the code below the countdown displays the same results as the output.  However, the first iteration is performed without a evaluation of the expression.  It's not until the countdown is 9 that the while part of the loop kicks in.

Friday, October 1, 2021

The beauty of Docker is that you can run a very lightweight image of another Linux distro on your host system.  In this post we will be pulling the latest image of CentOS into our docker container on our Ubuntu server.

Search This Blog