<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shrijan</title>
    <description>The latest articles on DEV Community by Shrijan (@shrijan00003).</description>
    <link>https://dev.to/shrijan00003</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F172582%2F4b67f887-4e2f-4258-9dec-e66aa22b01d8.jpeg</url>
      <title>DEV Community: Shrijan</title>
      <link>https://dev.to/shrijan00003</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shrijan00003"/>
    <language>en</language>
    <item>
      <title>Laravel Installation for Macbook. (Without docker)</title>
      <dc:creator>Shrijan</dc:creator>
      <pubDate>Fri, 14 May 2021 11:13:57 +0000</pubDate>
      <link>https://dev.to/shrijan00003/laravel-installation-for-macbook-without-docker-56al</link>
      <guid>https://dev.to/shrijan00003/laravel-installation-for-macbook-without-docker-56al</guid>
      <description>&lt;p&gt;Prerequisite: &lt;strong&gt;homebrew&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Disclaimer: This article is for the streamlined installation of laravel command-line tools with PHP and MySQL on MacBook. You can follow the installation and starting guide on laravel.com, I personally like the installation with &lt;code&gt;sail&lt;/code&gt;. But this is the traditional way of using laravel on Macbook. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;




&lt;h3&gt;
  
  
  PHP installation
&lt;/h3&gt;

&lt;p&gt;we will need to install PHP with homebrew, with command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew install php 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will install the latest version of php.&lt;/p&gt;

&lt;p&gt;If you have PHP installed already then simply update php&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew update php
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  MySql Installation
&lt;/h3&gt;

&lt;p&gt;The command is simple enough like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew install mysql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;once installation completed you can start mysql with brew services&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew services start mysql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;once mysql is running on your machine you can login to the mysql with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mysql -u root -p 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and enter your &lt;strong&gt;password&lt;/strong&gt; by default it will blank string. &lt;/p&gt;

&lt;p&gt;now you can use all of the commands for mysql. &lt;/p&gt;

&lt;h3&gt;
  
  
  Installation of the composer.
&lt;/h3&gt;

&lt;p&gt;For installing composer goto &lt;a href="https://getcomposer.org/download/"&gt;https://getcomposer.org/download/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and paste the following code found on the installation document.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you try to use the composer command it will not be found and the &lt;code&gt;composer.phar&lt;/code&gt; file is not executable. Follow the following command to make it executable and move it to the local bin folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ chmod +x composer.phar
$ mv composer.phar /usr/local/bin/composer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now try using composer&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You must be able to see available commands for the composer listed. &lt;/p&gt;

&lt;h3&gt;
  
  
  Laravel installer
&lt;/h3&gt;

&lt;p&gt;Install laravel with composer globally&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer global require laravel/installer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now try using laravel, the command will not found because composer is not been set on the zsh (our terminal)&lt;/p&gt;

&lt;p&gt;For zsh&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo 'export PATH="$HOME/.composer/vendor/bin:$PATH"' &amp;gt;&amp;gt;  ~/.zshrc
source ~/.zshrc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are bash then use&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo 'export PATH="$HOME/.composer/vendor/bin:$PATH"' &amp;gt;&amp;gt; ~/.bashrc
source ~/.bashrc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you have successfully installed the laravel command line tool. Now you can create laravel application with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;laravel new example-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can start creating your awesome application with laravel.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd example-app
php artisan serve
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Happy Coding....&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Docker and its basic commands</title>
      <dc:creator>Shrijan</dc:creator>
      <pubDate>Thu, 30 Apr 2020 15:12:47 +0000</pubDate>
      <link>https://dev.to/shrijan00003/docker-and-its-basic-commands-5gld</link>
      <guid>https://dev.to/shrijan00003/docker-and-its-basic-commands-5gld</guid>
      <description>&lt;h3&gt;
  
  
  What is docker?
&lt;/h3&gt;

&lt;p&gt;Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. (by- opensource.com)&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a docker container?
&lt;/h3&gt;

&lt;p&gt;A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a docker image?
&lt;/h3&gt;

&lt;p&gt;A Docker image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries, and settings.&lt;/p&gt;

&lt;p&gt;A Docker image is a file, comprised of multiple layers, that is used to execute code in a Docker container. When the Docker user runs an image, it can become one or multiple instances of that container.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Docker commands
&lt;/h2&gt;

&lt;p&gt;Here, we will look at some of the basic commands for developers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Docker Images
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# List available images in the local registry&lt;/span&gt;
&lt;span class="s"&gt;docker images&lt;/span&gt;

&lt;span class="c1"&gt;# Get image history &lt;/span&gt;
&lt;span class="s"&gt;docker image history &amp;lt;image-name/id&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;# Get all details about the images&lt;/span&gt;
&lt;span class="s"&gt;docker image inspect &amp;lt;image_name/id&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;# Remove image from the local registry&lt;/span&gt;
&lt;span class="c1"&gt;# We can use an alias like rm or rmi as well&lt;/span&gt;
&lt;span class="s"&gt;docker image remove &amp;lt;image-name/id&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;# Remove all unused images&lt;/span&gt;
&lt;span class="s"&gt;docker image prune&lt;/span&gt;

&lt;span class="c1"&gt;# Pull an image or a repository from a registry&lt;/span&gt;
&lt;span class="s"&gt;docker image pull &amp;lt;image/repository&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;# Push an image to a registry&lt;/span&gt;
&lt;span class="s"&gt;docker image push &amp;lt;repository-name:tag&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Docker container
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# All running containers&lt;/span&gt;
&lt;span class="s"&gt;docker container ls&lt;/span&gt;

&lt;span class="c1"&gt;# All containers&lt;/span&gt;
&lt;span class="s"&gt;docker container ls -a&lt;/span&gt;

&lt;span class="c1"&gt;# Stop the running container &lt;/span&gt;
&lt;span class="s"&gt;docker container stop &amp;lt;container-id&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;# Pause the running container&lt;/span&gt;
&lt;span class="s"&gt;docker container pause &amp;lt;container-id&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;# Unpause the running container&lt;/span&gt;
&lt;span class="s"&gt;docker container unpause &amp;lt;container-id&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;# Kill the docker container&lt;/span&gt;
&lt;span class="s"&gt;docker container kill &amp;lt;container-id&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;# Inspect the container &lt;/span&gt;
&lt;span class="s"&gt;docker container inspect &amp;lt;container-id&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;# Remove the unused container&lt;/span&gt;
&lt;span class="s"&gt;docker container prune&lt;/span&gt; 

&lt;span class="c1"&gt;# Remove one or more container&lt;/span&gt;
&lt;span class="s"&gt;docker container rm &amp;lt;container-id&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's try running &lt;br&gt;
&lt;code&gt;docker run -p 5000:5000 learntechfree/simple-express-app:0.0.1.RELEASE&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;What we just wrote is the shortcut for docker container command &lt;br&gt;
&lt;code&gt;docker container run -p 5000:5000 learntechfree/simple-express-app:0.0.1.RELEASE&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;On pausing&lt;/strong&gt; the container, that will not receive any request and that might cause the timeout if we try to access that docker container. In Linux, that will send the &lt;code&gt;SIGSTOP&lt;/code&gt; signal.&lt;/p&gt;

&lt;p&gt;Moreover, the difference between the &lt;code&gt;STOP&lt;/code&gt; and &lt;code&gt;KILL&lt;/code&gt; is, &lt;code&gt;STOP&lt;/code&gt; allows us to close the container gracefully and &lt;code&gt;KILL&lt;/code&gt; not. Internally &lt;code&gt;STOP&lt;/code&gt; executes the command &lt;code&gt;SIGTERM&lt;/code&gt; and if needed &lt;code&gt;SIGKILL&lt;/code&gt; and &lt;code&gt;KILL&lt;/code&gt; directly execute &lt;code&gt;SIGKILL&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Docker System
&lt;/h3&gt;

&lt;p&gt;Docker system basically has four basic commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Shows the docker disk usage&lt;/span&gt;
&lt;span class="s"&gt;docker system df&lt;/span&gt;

&lt;span class="c1"&gt;# Get real-time events from the server&lt;/span&gt;
&lt;span class="s"&gt;docker system events&lt;/span&gt;

&lt;span class="c1"&gt;# Display system-wide information&lt;/span&gt;
&lt;span class="s"&gt;docker system info&lt;/span&gt;

&lt;span class="c1"&gt;# Remove unused data, images, containers, networks, and build caches.&lt;/span&gt;
&lt;span class="c1"&gt;# Will be useful when you want to remove all unused things&lt;/span&gt;
&lt;span class="s"&gt;docker system prune&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Docker Stat
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;
&lt;span class="c1"&gt;# Display resource usage statistics&lt;/span&gt;
&lt;span class="s"&gt;docker stats &amp;lt;container-id&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;# or&lt;/span&gt;
&lt;span class="s"&gt;docker container stats &amp;lt;container-id&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This command is used to view the CPU usage, memory usage, memory limit of the container.&lt;/p&gt;

&lt;p&gt;We can explicitly set the limit for memory usage for that container with parameter -m.&lt;/p&gt;

&lt;p&gt;Check the following command where we have provided 512Mib of memory limit for the container.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker container run -p 5000:5000 -d -m 512m learntechfree/simple-express-app:0.0.1.RELEASE&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, if we do &lt;code&gt;docker stats &amp;lt;container-id&amp;gt;&lt;/code&gt; then you will get the limit for that container to 512mb only not the default 2GB. &lt;/p&gt;

&lt;p&gt;We can also say how much percentage of CPU we want to assign to our container or say how much part of CPU we want to provide to the container with cpu-quota parameter.&lt;/p&gt;

&lt;p&gt;The total CPU quota is of 100,000 or says a hundred thousand so that if we want to provide half of it then we  have to provide &lt;code&gt;--cpu-quota=50000&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Try,&lt;br&gt;
&lt;code&gt;docker container run -p 5000:5000 -d -m 512m --cpu-quota=50000 wordpress&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As only half of the CPU is available it might take a bit more time for a startup and process.&lt;/p&gt;

&lt;p&gt;Next: &lt;a href="https://dev.to/shrijan00003/creating-and-deploying-your-first-dockerized-web-application-3bhm"&gt;Creating our own docker image and deploy to docker hub&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Creating and deploying your first Dockerized web application</title>
      <dc:creator>Shrijan</dc:creator>
      <pubDate>Wed, 29 Apr 2020 16:38:05 +0000</pubDate>
      <link>https://dev.to/shrijan00003/creating-and-deploying-your-first-dockerized-web-application-3bhm</link>
      <guid>https://dev.to/shrijan00003/creating-and-deploying-your-first-dockerized-web-application-3bhm</guid>
      <description>&lt;h2&gt;
  
  
  PART-1: Creating a simple web application with NodeJs and express.
&lt;/h2&gt;

&lt;p&gt;For creating a simple node application we will be using express, a simple light weighted framework for NodeJs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create a folder on your project folder
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;mkdir simple-express-app&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Create NPM repository
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;npm init -y&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This should create a &lt;code&gt;package.json&lt;/code&gt; file in your root directory. The file must look like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"simple-express-app"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"index.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"echo &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;Error: no test specified&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt; &amp;amp;&amp;amp; exit 1"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"keywords"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"author"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"license"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ISC"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;we can edit this file for adding description, author, keywords for the project. You can edit whatever you want to edit though.&lt;/p&gt;

&lt;h3&gt;
  
  
  Add express dependency
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;npm i express&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will pull the express package from the NPM registry, make node_modules if not available and add in dependencies key of &lt;code&gt;package.json&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create &lt;code&gt;index.js&lt;/code&gt; file with express.
&lt;/h3&gt;

&lt;p&gt;The file might look like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;HOST&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0.0.0.0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`Hello from simple-express-app`&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;HOST&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Running on http://&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;HOST&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;No need to worry if you are unaware of node application, we will be looking in detail below: &lt;/p&gt;

&lt;p&gt;Create an express application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Create route for root get request and send the json response.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`Hell from simple-express-app`&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Listen to port and host&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;HOST&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Running on http://&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;HOST&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Run the project
&lt;/h3&gt;

&lt;p&gt;Now we can run the express application simply with &lt;code&gt;node index.js&lt;/code&gt; but make sure you have installed your node dependencies if that is missing run &lt;code&gt;npm install&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Verify if your project is running.
&lt;/h3&gt;

&lt;p&gt;Go to your browser and check the &lt;a href="http://localhost:5000/"&gt;url&lt;/a&gt; and we should get the &lt;code&gt;JSON&lt;/code&gt; output for the message.&lt;/p&gt;

&lt;p&gt;.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;/p&gt;
&lt;h2&gt;
  
  
  PART-2, Dockerizing the project
&lt;/h2&gt;

&lt;p&gt;For dockerizing the project we need to create &lt;code&gt;Dockerfile&lt;/code&gt;. Let's create &lt;code&gt;Dockerfile&lt;/code&gt; in your root.&lt;/p&gt;

&lt;p&gt;The Docker file will look like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; node:10-alpine&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . /app &lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; 5000&lt;/span&gt;
&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; node index.js&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Don't get afraid of the code herewith. We are going to discuss each part of it.&lt;/p&gt;

&lt;p&gt;The first line will define from what image we want to start building, here we are getting node of version 10 with &lt;code&gt;alpine&lt;/code&gt; which is the light Linux distribution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; node:10-alpine&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Next line will create a directory to hold the application code inside your image, this will be the working directory for your application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The third step will copy your application code with the current directory to &lt;code&gt;/app&lt;/code&gt; which is the working directory for our image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . /app &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The fourth step will run the command for installing dependency we will have in our node application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;RUN &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The next one will bind your app to port 5000 or whatever you have exposed herewith. We will use &lt;code&gt;EXPOSE&lt;/code&gt; instruction to have it mapped by &lt;code&gt;docker&lt;/code&gt; daemon.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; 5000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The last but not least, we will define the command to run our application using &lt;code&gt;CMD&lt;/code&gt;. Here we will be using &lt;code&gt;node index.js&lt;/code&gt; for starting our server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; node index.js&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we are ready to dockerize our project, let's build our first docker image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight docker"&gt;&lt;code&gt;docker build -t &amp;lt;your-username&amp;gt;/simple-express-app .

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that period sign (.) at the last of the docker build command that tells docker-daemon to build with files with the current directory.&lt;/p&gt;

&lt;p&gt;The -t flag lets us put a tag for our image so it will be easier to find and use with the &lt;code&gt;docker images&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;The user name is the docker-hub username, which will be used to push the images to the docker hub.&lt;/p&gt;

&lt;p&gt;Now find your image with &lt;code&gt;docker images&lt;/code&gt; command and you will get similar output. Make sure the new docker image is created with the tag we have provided for our first docker image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight docker"&gt;&lt;code&gt;$ docker images

&lt;span class="c"&gt;# Example&lt;/span&gt;
REPOSITORY                         TAG        ID              CREATED
node-alpine                        10         1934b0b038d1    5 days ago
&amp;lt;your username&amp;gt;/simple-express-app    latest     d64d3505b0d2    1 minute ago
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Run the image
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight docker"&gt;&lt;code&gt;docker run -p 5000:5000 -d &amp;lt;your-username&amp;gt;/simple-express-app
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here -p stands for publishing or expose the port 5000. This binds the port 5000 of the container where our express app will run with the 5000 port of the host machine.&lt;/p&gt;

&lt;p&gt;The -d tag will run the container in detached more or run the container in the background.&lt;/p&gt;

&lt;h4&gt;
  
  
  List and test if docker container is running
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# list the running container and get the id&lt;/span&gt;
&lt;span class="s"&gt;docker ps&lt;/span&gt;

&lt;span class="c1"&gt;# get logs for application&lt;/span&gt;
&lt;span class="s"&gt;docker logs &amp;lt;container_id&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;# test in the browser or &lt;/span&gt;
&lt;span class="s"&gt;curl -i localhost:5000&lt;/span&gt;

&lt;span class="c1"&gt;# If you want to stop the running container &lt;/span&gt;
&lt;span class="s"&gt;docker stop &amp;lt;container_id&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Publish
&lt;/h4&gt;

&lt;p&gt;Now let's publish our first docker image to docker hub. There are very simple steps to publish&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Login to docker hub&lt;/span&gt;
&lt;span class="c1"&gt;# however -p via CLI is not secure we can use &lt;/span&gt;
&lt;span class="c1"&gt;# cat ~/my_password.txt | docker login --username foo --password-stdin&lt;/span&gt;
&lt;span class="s"&gt;docker login -u &amp;lt;user-name&amp;gt; -p &amp;lt;password&amp;gt;&lt;/span&gt;


&lt;span class="c1"&gt;# Push to docker hub&lt;/span&gt;
&lt;span class="c1"&gt;# here 0.0.1.RELEASE is the release version&lt;/span&gt;
&lt;span class="c1"&gt;# Don't forget to replace learntechfree with your username&lt;/span&gt;

&lt;span class="s"&gt;docker push learntechfree/simple-express-app:0.0.1.RELEASE&lt;/span&gt; 

&lt;span class="c1"&gt;# Now visit the docker hub and check repository we will get the currently pushed images in your repository.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What's Next: Improving Layer Caching (Article coming soon).&lt;/p&gt;

&lt;p&gt;Reference: &lt;a href="https://docs.docker.com/"&gt;Documentation&lt;/a&gt;&lt;/p&gt;

</description>
      <category>docker</category>
      <category>webdev</category>
      <category>node</category>
      <category>dockerhub</category>
    </item>
    <item>
      <title>JavaScript Object Getter and Setter</title>
      <dc:creator>Shrijan</dc:creator>
      <pubDate>Sat, 04 Apr 2020 21:00:49 +0000</pubDate>
      <link>https://dev.to/shrijan00003/javascript-object-getter-and-setter-2f6d</link>
      <guid>https://dev.to/shrijan00003/javascript-object-getter-and-setter-2f6d</guid>
      <description>&lt;p&gt;JavaScript object may have multiple properties and methods to deal with static data along with dynamic calculation. There are many ways to achieve these functionality and we are going to explore some of them here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Object Literals (Old School way and most popular)
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;student&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;_name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ram&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nameParam&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nameParam&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// should return =&amp;gt;  Ram&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now lets set the name and get later with the same example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;student&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;_name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;shrijan&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nameParam&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nameParam&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Shyam&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// should return =&amp;gt; Shyam&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There is no doubt that we can simply can get with &lt;code&gt;student._name&lt;/code&gt; as well, we are considering that we are not exposing properties publicly. Please help me finding use cases below in comment whether we don't have to expose properties like in above.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using &lt;code&gt;get&lt;/code&gt; and &lt;code&gt;set&lt;/code&gt; syntax
&lt;/h2&gt;

&lt;p&gt;According to MDN &lt;code&gt;get&lt;/code&gt; and &lt;code&gt;set&lt;/code&gt; syntax binds an object property to a function that will be called when that property is looked up.&lt;/p&gt;

&lt;p&gt;Sometimes we want to access the property with dynamically calculated value without implementing explicit method calls. Where &lt;code&gt;getter&lt;/code&gt; and &lt;code&gt;setter&lt;/code&gt; comes in play.&lt;/p&gt;

&lt;p&gt;Let us take the same example of student&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;student&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="na"&gt;_firstName&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ram&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;_lastName&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Shrestha&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

  &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_firstName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_lastName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// should return 'Ram Shrestha'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Whats new here ?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;get&lt;/code&gt; keyword is used to define or bind &lt;code&gt;fullName&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;accessing &lt;code&gt;fullName&lt;/code&gt; as property not as the function.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You might be thinking what if we want to set the first name and last name with providing full name. Yes, your guess is correct we will be using set keyword to set property. Lets expand the example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;student&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="na"&gt;_firstName&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ram&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;_lastName&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Shrestha&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

  &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_firstName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_lastName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;

  &lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;splittedNames&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;_first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_second&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;splittedNames&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_first&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_second&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// should return 'Ram Shrestha'&lt;/span&gt;

&lt;span class="nx"&gt;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shyam Sundar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// should return 'Shyam Sundar'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Again What's new here ?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;set&lt;/code&gt; keyword to define setter&lt;/li&gt;
&lt;li&gt;accessing with &lt;code&gt;fullName&lt;/code&gt; property to set data not function.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Wait&lt;/strong&gt;, why are we doing so much calculating for simply setting up first name and last name why not &lt;code&gt;fullName(firstName, lastName)&lt;/code&gt; at a same time? I was thinking same but &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Remember: &lt;code&gt;get&lt;/code&gt; can not have any parameter and &lt;code&gt;set&lt;/code&gt; can have only one.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Using Object.defineProtpery method
&lt;/h2&gt;

&lt;p&gt;According to MDN : The static method &lt;code&gt;Object.defineProperty()&lt;/code&gt; defines a new property directly on an object, or modifies an existing property on an object, and returns the object.&lt;/p&gt;

&lt;p&gt;lets try to achieve same functionality with following example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;student&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;_firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ram&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;_lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shrestha&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;defineProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;student&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fullName&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_firstName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_lastName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;set&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;splittedNames&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;_first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_second&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;splittedNames&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_first&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_second&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// should return 'Ram Shrestha'&lt;/span&gt;

&lt;span class="nx"&gt;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shyam Sundar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// should return 'Shyam Shrestha'&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Thank you :) &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>getter</category>
      <category>setter</category>
      <category>object</category>
    </item>
  </channel>
</rss>
