<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Muhammad Abdullah Khan on Medium]]></title>
        <description><![CDATA[Stories by Muhammad Abdullah Khan on Medium]]></description>
        <link>https://medium.com/@abdullah.khan10032?source=rss-1509feea89b0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*stqTjkPXWaLT2SNPTHJotg.jpeg</url>
            <title>Stories by Muhammad Abdullah Khan on Medium</title>
            <link>https://medium.com/@abdullah.khan10032?source=rss-1509feea89b0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 18:02:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@abdullah.khan10032/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[JavaScript in the Background]]></title>
            <link>https://medium.com/@abdullah.khan10032/javascript-in-the-background-895082bb1d08?source=rss-1509feea89b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/895082bb1d08</guid>
            <category><![CDATA[javascript-basic-concept]]></category>
            <category><![CDATA[javascript-development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Muhammad Abdullah Khan]]></dc:creator>
            <pubDate>Wed, 07 Feb 2024 06:14:14 GMT</pubDate>
            <atom:updated>2024-02-07T06:19:07.065Z</atom:updated>
            <content:encoded><![CDATA[<p>JavaScript is getting more and more popular, and its support on many levels in their stack: front-end, back-end, hybrid apps, embedded devices, and much more.</p><p><strong>What is JavaScript?</strong></p><p>JavaScript is a single-threaded language that uses a callback queue.</p><p>In this article, we will go through the basic concepts in detail and explain how JavaScript works.</p><p>If you are new to JavaScript, this article will help you understand the core concepts of JavaScript.</p><p><strong>The JavaScript Engine:</strong></p><p>JavaScript Engine is a computer program that executes JavaScript code. JavaScript Engine receives JavaScript files and converts them into machine-understandable language sends it to the machine and tells the machine what to do. It works like a <strong>translator</strong>.</p><p>The most commonly use engine is <strong>Google V8</strong> <strong>Engine</strong>. V8 is open source and used inside Chrome and Node.js developed by the Chromium Project of Google Chrome.</p><p>The JavaScript Engine consists of two components:</p><ul><li><strong>Memory Heap: </strong>It is a place where Memory allocation happens and stores/writes information. It is also known as a dynamic memory.</li><li><strong>Call Stack: </strong>It tells where your code is in execution (what function is currently running). It keeps track of where we are in the code so that we can run the code in order.</li></ul><p><strong>Call Stack:</strong></p><p>JavaScript is a single-threaded programming language. This means that it has one call stack. Therefore, it can do one thing at a time.</p><p>The Call Stack is a data structure that records basically where we are in the program. If we go into a function, we put a record at the top of the stack. When we return from the function, we pull the topmost element from the stack and find ourselves where we called this function from. This is all the stack can do.</p><pre>function multiply(x, y) {<br>   return x * y;<br>}<br><br>function printSquare(x) {<br>   var s = multiply(x, x);<br>   console.log(s);<br>}<br><br>printSquare(5);</pre><p>In the given example at first, we run a function printSquare(5) it is added on the stack, and inside it, there is another function multiply(x,y) that is also added at the top of the stack and calls stack first to resolve the top most function and return that value and then move down. Once the function is resolved it also removes it from the call stack.</p><p><strong>Stack Overflow:</strong></p><p>This happens when you reach the maximum Call Stack size. This commonly occurs due to recursion or by call function many times and not removing it from memory. Each entry in the call stack is a call <strong>stack frame.</strong></p><p>For example:</p><pre>function foo() {<br>  foo();<br>}<br><br>foo();</pre><p>At some point, however, the amount of data about the function calls will exceed the size of the call stack and the browser decides to intervene, giving an error:</p><p><strong>JavaScript Runtime:</strong></p><p>If we talk about the use of JavaScript in the browser, then there are APIs, for example, functions <strong>setTimeout</strong> that almost every JS developer uses. However, these APIs are not provided by the engine.</p><p>Where do they come from? Reality looks a little more complicated than it might seem at first glance.</p><p><strong><em>The engine, the event loop, the queue of callback functions, and APIs provided by the browser.</em></strong></p><p>Web APIs work in the background. Web APIs are asynchronous which means you can instruct them to do something in the background and return the data once it is done. Mean we can continue working on the call stack and execute function ns.</p><p>Call Stack executes the functions, when any function comes that contains Web APIs (like <strong>setTimeout, setInterval</strong>) they send it to Web APIs. The Web APIs resolve that function and return the data once it is resolved Web API sends the data to the Callback Queue and the Event loop checks call stack is free or not when the call stack is free it pushes the callback queue stack in the call stack.</p><p><strong>Open browser -&gt; go to console -&gt; write window/this (It will show you all web APIs provided by the browser).</strong></p><p><strong>Execution Context:</strong></p><p>Execution Context is defined as the environment in which the JavaScript code is executed. By environment, I mean the value of these, variables, objects, and functions JavaScript code has access to at a particular time.</p><p>When JavaScript sees a function or () it runs that function creates an execution context of that function adds it to the call stack and runs that function</p><p>For Example:</p><pre>function printName(){<br>  return &quot;Abdullah Khan&quot;<br>}<br><br>function findName(){<br>  return printName()<br>}<br><br>function sayMyName(){<br>  return findName()<br>}<br><br>sayMyName()</pre><p>when a JavaScript engine sees a function it runs that function and creates an execution context, first, it creates an execution context <strong>sayMyName()</strong> adds it to the call stack, and tries to run this function and sees that this function is calling another function. So it creates a new execution context because it sees another function <strong><em>findName()</em></strong> and pushes it on the stack and when it runs this function JavaScript sees another function <strong><em>printName()</em></strong> It creates another execution context and pushes it on the stack and runs it now this time <strong><em>printName()</em></strong> return string <strong>“Abdullah Khan”</strong> after that function return value JavaScript remove it from the stack and move down and remove <strong><em>findName()</em></strong> because it also return function and at last also remove <strong><em>sayMyName()</em></strong> and all execution context popped off and return string on the screen.</p><p>Whenever code is run in JavaScript it runs inside of an execution context. Anytime we run code in JavaScript, it will always be a part of an execution context. JavaScript creates an execution context named <strong>global()</strong> it is a base execution context.</p><p><strong>Garbage Collection:</strong></p><p>JavaScript is Garbage Collected language. Like when we call a function it is added to the call stack and objects and variables are created and stored in memory. When that function is executed it is removed from the call stack and all its objects and variables are also removed from memory by garbage collection.</p><p><strong>Single-threaded:</strong></p><p><em>Only one set of instructions can be executed at a time.</em> Just a simple example if I open any browser write an <strong>alert(“Hello”) </strong>in its browser console and run it, an alert message is open and I can’t do anything until I close that alert.</p><p><strong>Hoisting:</strong></p><p>Hoisting is the behavior of moving the variables and function declarations to the top of their respective environments during the compilation phase. Variable are partially hoisted and function declarations are hoisted.</p><p>For Example:</p><pre>console.log(name)<br>output: undefined<br><br>city()<br>output: Lahore<br><br>var name= &quot;Abdullah&quot;<br><br>function city(){<br>  return &quot;Lahore&quot;<br>}</pre><p>name is undefined because variables are partially hoisted. When JavaScript sees the variable <strong>“name”</strong> it moves it to the top of their environment and assigns it undefined and it on memory.</p><p>In the case of function, functions are completed JavaScript moves the complete function on top of its environment and assigns a location in memory during the creation phase.</p><p><strong>Hoisted Functions:</strong></p><ul><li><strong>Function Expression: </strong>Function expression is not hoisted. This function is when hoisted or moved to the top of their environment it becomes equal to <strong>undefined</strong> because the function is assigned to a variable.</li></ul><pre>expression(); //Output: &quot;TypeError: expression is not a function<br><br>var expression = function() {<br>  console.log(&#39;Will this work?&#39;);<br>};</pre><ul><li><strong>Function Declaration: </strong>These are hoisted and completed.</li></ul><pre>hoisted(); // Output: &quot;This function has been hoisted.&quot;<br><br>function hoisted() {<br>  console.log(&#39;This function has been hoisted.&#39;);<br>};</pre><p><strong>Function Invocation:</strong></p><p>JavaScript Function Invocation is used to execute the function code and it is common to use the term “call a function” instead of “invoke a function”. The code inside a function is executed when the function is invoked.</p><pre>function myFunction(){<br>  console.log(&quot;HELLO&quot;)<br>}</pre><p><strong><em>myFucntion() -&gt;</em></strong><em> </em>Calling function, after calling a function execution context is created and pushed on the stack.</p><p><strong>this:</strong></p><p>“<em>this is the object that the function is a property of.”</em></p><p>What does that mean?</p><p>Let’s have an example:</p><pre>function myFunc(){<br>  console.log(this)<br>}<br><br>myFunc()</pre><p>when we run this function, it returns the window object because right now this === window. When we run myFunc() there is nothing on the left side of myFunc() so it becomes a window.myFunc() so the myFunc() is a property of the window object, so that is why this is the window object.</p><p>Let’s have another example:</p><pre>const obj = {<br>  name: &quot;Abdullah&quot;<br>  sing() {<br>    return this.name + &quot;sing&quot;<br>  }<br>}<br><br>obj.sing() output: Abdullah sing</pre><p>now the left side of sing() is obj so this inside sing belongs to <strong>obj</strong> object.</p><p><strong>Conclusion</strong></p><p>In general terms, in this article, we looked at the design of the JS engine, runtime mechanisms, and the call stack. Understanding the concepts presented here can improve the quality of the code.</p><p>If you found this post informative, please feel free to send me a connection request. If you found something missing, or wrong, please let me know in the comments so that everybody stays on the same page.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=895082bb1d08" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploying Laravel App to AWS EC2 with Apache]]></title>
            <link>https://medium.com/@abdullah.khan10032/deploying-laravel-app-to-aws-ec2-with-apache-0270136e979a?source=rss-1509feea89b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/0270136e979a</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[apache]]></category>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[aws-ec2]]></category>
            <category><![CDATA[laravel]]></category>
            <dc:creator><![CDATA[Muhammad Abdullah Khan]]></dc:creator>
            <pubDate>Tue, 30 Jan 2024 06:45:32 GMT</pubDate>
            <atom:updated>2024-01-30T07:26:19.561Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/892/1*g9ZihK73HCLHcCeIuKQLHQ.jpeg" /></figure><p>In this Article, We will Learn How to deploy a Laravel project on an AWS EC2 (Elastic Cloud Computing ) Instance with Apache.</p><p><strong>Prerequisites:</strong></p><ul><li>AWS account</li><li>AWE EC2 (Elastic Cloud Computing) Ubuntu instance</li><li>Your Laravel project</li></ul><h4>Steps to Deploy Laravel Project on EC2:</h4><p>Connect to the EC2 instance from SSH using your private key ( Key Pair ).</p><h4>Install PHP and Apache:</h4><p><strong>Update your libraries:</strong></p><pre>$ sudo apt-get update</pre><p><strong>Install Apache2:</strong></p><pre>$ sudo apt-get install apache2</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/890/1*zuYcDs7M9zGrQkGQqiJ1mQ.png" /></figure><p>Now that we installed Apache successfully, to check it copy the IPv4 Public IP Address of your EC2 instance and hit on the browser. If you see a screen something like below, you are good to go. The Apache server is working!</p><p><strong>Install PHP and useful packages:</strong></p><pre>$ sudo apt-get install php<br>$ php - version<br>$ sudo apt-get install php8.0-cli php8.0-common php8.0-curl php8.0-gd php8.0-json php8.0-mbstring php8.0-intl php8.0-mysql php8.0-xml php8.0-zip</pre><p>Usually, when a user requests a directory from the web server, Apache will first look for a file named <strong>index.html</strong>. If you want to change Apache to serve PHP files rather than others, move <strong>index.php</strong> to the first position in the <strong>dir.conf</strong> file as shown below.</p><pre>$ sudo vi /etc/apache2/mods-enabled/dir.conf</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3IiWSeLGJs19C5z1vUnEgA.png" /></figure><h4><strong>Install MySQL Server:</strong></h4><pre>$ sudo apt-get install mysql-server</pre><p>This will install mysql server but this will not prompt anything to configure mysql.</p><p><strong>Configuring MySQL:</strong></p><pre>$ sudo mysql_secure_installation</pre><p>To use a password to connect to MySQL as <strong>root</strong>, you will need to switch its authentication method from <strong>auth_socket</strong> to <strong>mysql_native_password</strong> by using the following commands.</p><pre>$ sudo mysql</pre><p>To configure the <strong>root</strong> account to authenticate with a password, run the following ALTER USER command. Note that this command will change the <strong><em>root</em></strong> password you set in <strong><em>mysql_secure_installation</em>.</strong></p><pre>mysql&gt; ALTER USER &#39;root&#39;@&#39;localhost&#39; IDENTIFIED WITH mysql_native_password BY &#39;yourpassword&#39;;</pre><p>Then, run <strong>FLUSH PRIVILEGES</strong> which tells the server to reload the grant tables and put your new changes into effect:</p><pre>FLUSH PRIVILEGES;</pre><p>Now You are done with setting up your MySQL server on your EC2. you can use your credentials in your Laravel project.</p><h4><strong>Install Composer:</strong></h4><p>Composer provides an <a href="https://getcomposer.org/installer">installer</a> script written in PHP. We’ll download it, verify that it’s not corrupted, and then use it to install Composer.</p><p>Make sure you’re in your home directory, then retrieve the installer using curl:</p><pre>$ cd ~<br>$ curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php</pre><p>Next, we’ll verify that the downloaded installer matches the SHA-384 hash for the latest installer found on the <a href="https://composer.github.io/pubkeys.html">Composer Public Keys / Signatures</a> page. To facilitate the verification step, you can use the following command to programmatically obtain the latest hash from the Composer page and store it in a shell variable:</p><pre>$ HASH=` curl -sS https://composer.github.io/installer.sig`</pre><p>If you want to verify the obtained value, you can run:</p><pre>$ echo $HASH<br><br>Output<br>E0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a</pre><p>To check the installer is verified:</p><pre>$ php -r &quot;if (hash_file(&#39;SHA384&#39;, &#39;/tmp/composer-setup.php&#39;) === &#39;$HASH&#39;) { echo &#39;Installer verified&#39;; } else { echo &#39;Installer corrupt&#39;; unlink(&#39;composer-setup.php&#39;); } echo PHP_EOL;&quot;<br><br>Output<br>Installer verified</pre><p>If the output says Installer corrupt, you’ll need to download the installation script again and double-check that you’re using the correct hash. Then, repeat the verification process. When you have a verified installer, you can continue.</p><p>To install Composer globally, use the following command which will download and install Composer as a system-wide command named composer, under /usr/local/bin:</p><pre>$ sudo php /tmp/composer-setup.php - install-dir=/usr/local/bin - filename=composer</pre><p>You’ll see output similar to this:</p><pre>Output<br>All settings are correct for using Composer<br>Downloading…<br>Composer (version 2.2.9) successfully installed to: /usr/local/bin/composer<br>Use it: php /usr/local/bin/composer</pre><p>To check composer installed successfully</p><pre>$ composer</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9Bdk4iVOcBDr2F8xBuBUog.png" /></figure><h4><strong>Install Git:</strong></h4><pre>$ sudo apt-get install git-core</pre><p><strong>Clone Your Repository:</strong></p><p>Change to <strong>/var/www/html</strong> directory and clone your Laravel Project there using the following commands.</p><pre>$ cd /var/www/html<br>$ sudo git clone https://github.com/username/reponame.git<br>$ cd reponame</pre><p><strong>Adding .env File to your Laravel Project:</strong></p><pre>$ sudo nano .env</pre><p><strong>Installing Libraries In Project:</strong></p><pre>$ sudo composer install</pre><p><strong>Read Write Permissions for your Project:</strong></p><pre>$ sudo chmod 777 -R storage/<br>$ sudo chmod 777 -R bootstrap/</pre><p><strong>Apache Configuration:</strong></p><p>By Default Apache opens/var/www/html/index.html Now we have to open our Laravel project when we go to the public IP of the EC2.<br>Go to /etc/apache2/sites-available/000-default.conf and change the DocumentRoot as shown below.</p><pre>$ sudo nano /etc/apache2/sites-available/000-default.conf</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1020/1*4fQCruHT31wzfBU2npcOxw.png" /></figure><p><strong>Restart your Apache:</strong></p><pre>$ sudo systemctl restart apache2</pre><p>Now you have successfully deployed your Laravel project on AWS, Which you can easily access through your public IPV4 IP Address.</p><h4><strong>API routes: 404 Not found:</strong></h4><p>An Error that you might get of 404 Not Found. To resolve this error follow the following steps.</p><p>In the file /etc/apache2/apache2.conf, add the following code :</p><pre>&lt;Directory /var/www/html/app/public&gt;<br>    Options Indexes FollowSymLinks<br>    AllowOverride All<br>    Require all granted<br>&lt;/Directory&gt;</pre><p>In the file /etc/apache2/sites-enabled/000-default.conf, change the document root path to your project path :</p><p>DocumentRoot /var/www/html/app/public</p><p><strong>And enable the rewrite apache2 mode:</strong></p><pre>$ sudo a2enmod rewrite</pre><p><strong>Restart Apache to apply changes:</strong></p><pre>$ sudo systemctl restart apache2</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0270136e979a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[AWS Private vs Public vs Elastic IP]]></title>
            <link>https://medium.com/@abdullah.khan10032/aws-private-vs-public-vs-elastic-ip-bbc56c85fafd?source=rss-1509feea89b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/bbc56c85fafd</guid>
            <dc:creator><![CDATA[Muhammad Abdullah Khan]]></dc:creator>
            <pubDate>Mon, 22 Aug 2022 13:44:33 GMT</pubDate>
            <atom:updated>2022-08-22T13:44:33.555Z</atom:updated>
            <content:encoded><![CDATA[<p>Networking has two sorts if IPs:</p><p><strong><em>IPv4</em></strong><em>: 192.168.1.1<br></em><strong><em>IPv6</em></strong><em>: 1900:4545:1:100:f8fa:f2op:12ef</em></p><p>Let talk about Public IP, there are different type of IP like,</p><p>If we have a web server that is public IP <em>79.216.59.75</em> and we have another server with have another IP <em>211.139.37.43</em>, these server is accessible on www using this IP and using these public IPs these server can talk with each other.</p><p>Lets consider we have an company A who have an private network like <em>192.168.0.1/22</em>. Private network have some private IP ranges and all the computers inside that private network can talk to each other using the private IP. But these IPs are not accessible on WWW, to make it accessible on WWW we use <a href="https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Internet_Gateway.html">Internet Gateway</a> (public). Internet Gateway enable private network to talk to other server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dZ_OGyfIqFPDLisLdfa0dw.png" /></figure><p><strong>Private / Public / Elastic IP Difference :</strong></p><p><strong>Private IP (IPv4):</strong></p><ul><li>Public IP means the machine can be identified on internet (WWW).</li><li>Must be unique across the whole web (not two machine can have the same public IP).</li><li>Can be geo-location easily.</li></ul><p><strong>Public IP (IPv4):</strong></p><ul><li>Private IP means the machine can be only be identified on a private network only.</li><li>The IP must be unique across the private network.</li><li>But two different private networks (two companies) can have the same IPs.</li><li>Machines connect to WWW using an internet gateway (a proxy).</li><li>Only a specified range of IPs can be used as a private IP.</li></ul><p><strong>Elastic IP:</strong></p><ul><li>With the Elastic IP address, you can mask the failure of an instance or software by rapidly remapping the address to another instance in your account.</li><li>You can only have 5 elastic ip in you account (you can ask AWS to increase that).</li><li>Overall, try to avoid using elastic IP:</li><li>1) They often reflect poor architectural decision.</li><li>2) Instead use a random public IP and register a DNS name to it.</li><li>3) Or as we’ll see later, use a Load Balancer and don’t use a public IP.</li></ul><p><strong>Private and Public IP (IPv4) In AWS EC2 — Hands On</strong></p><p>By default, you EC2 machine comes with</p><ul><li>A private IP for the internal AWS Network.</li><li>A public IP for the WWW</li></ul><p>When we are doing SSH into our EC2 machines:</p><ul><li>We can’t use a private IP, because we are not in the same network.</li><li>We can only use the public IP.</li><li>If your machine is stopped and then started the public IP can change</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bbc56c85fafd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dockerize a Node app and deploy to Heroku]]></title>
            <link>https://medium.com/@abdullah.khan10032/dockerize-a-node-app-and-deploy-to-heroku-3cfab23c6c67?source=rss-1509feea89b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/3cfab23c6c67</guid>
            <category><![CDATA[containers]]></category>
            <category><![CDATA[heroku]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[image]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Muhammad Abdullah Khan]]></dc:creator>
            <pubDate>Tue, 03 May 2022 12:20:47 GMT</pubDate>
            <atom:updated>2022-08-18T09:28:16.394Z</atom:updated>
            <content:encoded><![CDATA[<h3>Prerequisites</h3><ul><li>Node</li><li>npm</li><li>Docker</li></ul><h3>1. Create a node.js app</h3><p>First, let’s create a simple node.js(express) application, to get started with express.</p><p>Create project by running <em>npm init -y </em>and copy paste the following lines in <em>package.json </em>file.</p><pre>{<br>  “name”: “heroku_docker”,<br>  “version”: “1.0.0”,<br>  “description”: “Deploy Dockerized NodeJS Image app on Heroku”,<br>  “main”: “index.js”,<br>  “scripts”: {<br>     “start”: “node index.js”,<br>     “dev”: “nodemon index.js”<br>  },<br>  “author”: “”,<br>  “license”: “ISC”,<br>  “dependencies”: {<br>    &quot;dotenv&quot;: &quot;^16.0.0&quot;,<br>    &quot;express&quot;: &quot;^4.18.1&quot;,<br>    &quot;nodemon&quot;: &quot;^2.0.16&quot;<br>  }<br>}</pre><p>Create a file named <em>index.js</em> which is our express app, and add the following.</p><pre>const express = require(&quot;express&quot;);<br>require(&quot;dotenv&quot;).config();</pre><pre>const PORT = process.env.PORT || 3000;</pre><pre>const app = express();</pre><pre>app.use(&quot;/&quot;, (req, res, next) =&gt; {<br>  res.send(&quot;Node JS backend is running&quot;);<br>});</pre><pre>app.listen(PORT, () =&gt; {<br>  console.log(`Server is listing on PORT : ${PORT}`);<br>});</pre><p>Create .env file and add PORT</p><pre>PORT=3000</pre><p>Now, let’s try to run our app in the terminal,</p><pre>$ npm install<br>$ npm start</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/314/1*5xJ6C2P1MJ9nzrSjj_amYQ.png" /></figure><p>Open Browser and type localhost:3000</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/336/1*63W7aGsxVmYmLHzzsTgL7g.png" /></figure><h3>2. Set up the Dockerfile</h3><p>The next step is to create a Dockerfile.</p><p>If you haven’t installed docker in your system, visit<a href="https://docs.docker.com/get-docker/"> <strong>here </strong></a>to download it.</p><ul><li><strong>Dockerfile</strong>: you can think of the Dockerfile as a receipt — it includes instructions on how to create a Docker image</li><li><strong>Docker image</strong>: the output of the Dockerfile run — this is the runnable unit</li></ul><p>Add the following in <strong>Dockerfile</strong></p><pre>FROM node:alpine<br># CREATE APP DIRECTORY<br>RUN mkdir -p /app<br>WORKDIR /app<br># INSTALL DEPENDENCIES<br>COPY package*.json ./<br>RUN npm install<br>COPY . .<br># Exports<br>EXPOSE 3000<br>CMD [&quot;npm&quot;,&quot;start&quot;]</pre><blockquote>FROM node:alpine —From the base image <strong>node, </strong>with <strong>alpine</strong> variant.</blockquote><blockquote>WORKDIR — We are mentioning that the directory called app is going to hold our project. If the directory specified doesn’t exist in the image, it is newly created.</blockquote><blockquote><em>COPY package*.json ./ — </em>We are copy our <em>package.json</em> and <em>package-lock.json </em>to our workdir.</blockquote><blockquote><em>RUN npm install — </em>This command installs the dependencies mentioned in our package.json</blockquote><blockquote><em>CMD [ &quot;npm&quot;, &quot;start&quot;] — </em>Executes <em>npm start </em>, which is the command to run our app</blockquote><h3>3. Login to Heroku and create app</h3><p>Run the following commands to login in heroku and create app on heroku. Copy the name of Heroku App.</p><pre>$ heroku login<br>$ heroku container:login<br>$ heroku create</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-2cZOEZfawX_zuPuX6Qj2g.png" /></figure><h3>4. Build the image using Dockerfile</h3><p>Being in your project directory, run the following command.</p><pre>$ docker build -t registry.heroku.com/&lt;HEROKU_APP_NAME&gt;/web .<br>e.g docker build -t registry.heroku.com/heroku-docker01/web .</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/926/1*n16uMWTaG4x6cRAmeeKaDA.png" /></figure><pre>$ docker push registry.heroku.com/&lt;HEROKU_APP_NAME&gt;/web<br>e.g docker push registry.heroku.com/heroku-docker01/web</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/902/1*RdpQ0H6wDBiDD4u9YYhByA.png" /></figure><pre>$ heroku container:release web -a &lt;HEROKU_APP_NAME&gt;<br>e.g heroku container:release web -a heroku-docker01</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*cDJd2h1MbDW9tordiPtlRA.png" /></figure><p>After Image release on heroku app click on open app in heroku.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e-LnUQ9qzY7bhwXzpP3B2Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*oa2476mH2UA9fA4yRFUcNQ.png" /></figure><p>Github repo — <a href="https://github.com/Abdullah0332/heroku-docker">https://github.com/Abdullah0332/heroku-docker</a></p><p>Happy Learning!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3cfab23c6c67" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>