<?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>Forem: Prince</title>
    <description>The latest articles on Forem by Prince (@pk504b).</description>
    <link>https://forem.com/pk504b</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%2F2653052%2Fad1fc274-8585-4016-8068-bd1da721bdd5.jpg</url>
      <title>Forem: Prince</title>
      <link>https://forem.com/pk504b</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/pk504b"/>
    <language>en</language>
    <item>
      <title>Two ways to access localhost on your phone ✌️</title>
      <dc:creator>Prince</dc:creator>
      <pubDate>Tue, 07 Jan 2025 17:36:39 +0000</pubDate>
      <link>https://forem.com/pk504b/two-ways-to-access-localhost-on-your-phone-a26</link>
      <guid>https://forem.com/pk504b/two-ways-to-access-localhost-on-your-phone-a26</guid>
      <description>&lt;p&gt;If you're working on a project on your laptop, chances are you would want to open it on your phone as well. Most browser do have &lt;em&gt;Device Emulation&lt;/em&gt; to test app's design and responsiveness on different screen sizes but it still does not give the full picture. For example, laptop and mobile have very different pixel density which can cause variations. And, more importantly, you cannot test for touch responses on laptop so well. So, here are two ways in which you can expose your localhost and access it on your phone.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjekxopd49bimpovwamdr.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjekxopd49bimpovwamdr.jpeg" alt="phone &amp;amp; laptop" width="800" height="533"&gt;&lt;/a&gt;&lt;br&gt;
&lt;small&gt;&lt;em&gt;Image credit: Photo by &lt;a href="https://www.pexels.com/photo/person-hold-gold-htc-android-smartphone-in-front-of-macbook-pro-1181216/" rel="noopener noreferrer"&gt;Christina Morillo&lt;/a&gt;&lt;/em&gt;&lt;/small&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Method 1: No third-party app or library required
&lt;/h2&gt;

&lt;p&gt;The simplest way to access your localhost from your phone is when both your laptop/desktop (where your app is running) and your mobile device are connected to the same Wi-Fi network. (It may not work on public networks). In this case, you can use need computer's &lt;strong&gt;local IP address&lt;/strong&gt; and the port on which the app is running. You can then go to &lt;code&gt;http://&amp;lt;ip-address&amp;gt;:&amp;lt;port&amp;gt;&lt;/code&gt; on your phone to see the app on your phone.&lt;/p&gt;
&lt;h3&gt;
  
  
  Steps:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Find your local IP address on your computer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;For Windows:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Open Command Prompt (press &lt;code&gt;Win + R&lt;/code&gt;, type &lt;code&gt;cmd&lt;/code&gt;, and press Enter).&lt;/li&gt;
&lt;li&gt;Type &lt;code&gt;ipconfig&lt;/code&gt; and press Enter.&lt;/li&gt;
&lt;li&gt;Look for the &lt;strong&gt;IPv4 Address&lt;/strong&gt; under your active network adapter.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;For Mac/Linux:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Open a terminal.&lt;/li&gt;
&lt;li&gt;Type &lt;code&gt;ifconfig&lt;/code&gt; (or &lt;code&gt;ip a&lt;/code&gt; on Linux) and press Enter.&lt;/li&gt;
&lt;li&gt;Look for the &lt;strong&gt;inet&lt;/strong&gt; address under the active network adapter (usually &lt;code&gt;en0&lt;/code&gt; for Mac or &lt;code&gt;eth0&lt;/code&gt; for Linux).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Note the port on which your app is running:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For example, if you're running a Node.js app or a web server, it might be running on &lt;code&gt;localhost:3000&lt;/code&gt; so the port number is &lt;code&gt;3000&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Access the app on your phone:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open the browser on your mobile device.&lt;/li&gt;
&lt;li&gt;In the address bar, type your computer's local IP address followed by the port number.

&lt;ul&gt;
&lt;li&gt;Example: &lt;code&gt;http://192.168.1.2:3000&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Your app should now be accessible from your mobile device!&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;


&lt;h2&gt;
  
  
  Method 2: Using Cloudflared
&lt;/h2&gt;

&lt;p&gt;What if your laptop and phone are on different networks (e.g., one is on Wi-Fi, and the other is on mobile data)? In this case, it's not as simple to use your local IP address. Instead, you can use &lt;a href="https://developers.cloudflare.com/pages/how-to/preview-with-cloudflare-tunnel/" rel="noopener noreferrer"&gt;Cloudflared&lt;/a&gt; to create a secure tunnel between your local environment and the internet, making your localhost accessible from anywhere.&lt;/p&gt;
&lt;h3&gt;
  
  
  What is Cloudflared?
&lt;/h3&gt;

&lt;p&gt;Cloudflared is a command-line tool developed by Cloudflare that allows you to create a secure tunnel to your localhost, even if your devices are not on the same network. This tool is part of Cloudflare’s &lt;strong&gt;Zero Trust&lt;/strong&gt; security model, which means it protects your data while ensuring secure and easy access to your local apps.&lt;/p&gt;
&lt;h3&gt;
  
  
  Steps:
&lt;/h3&gt;
&lt;h4&gt;
  
  
  1. Install Cloudflared
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;For macOS:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew &lt;span class="nb"&gt;install &lt;/span&gt;cloudflared
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;For Windows:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;winget install --id Cloudflare.cloudflared
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;For Linux:&lt;/strong&gt;
You can download and install Cloudflared by &lt;a href="https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/downloads/#linux" rel="noopener noreferrer"&gt;downloading&lt;/a&gt; the latest release directly  or via their &lt;a href="https://pkg.cloudflare.com/index.html" rel="noopener noreferrer"&gt;repository&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  2. Create a Tunnel to Your Localhost
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Once the package is installed, run the following command to create a tunnel:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cloudflared tunnel &lt;span class="nt"&gt;--url&lt;/span&gt; http://localhost:3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Replace &lt;code&gt;3000&lt;/code&gt; with whatever port your app is running on.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cloudflared will generate a URL (e.g., &lt;code&gt;https://your-tunnel-id.trycloudflare.com&lt;/code&gt;) that will tunnel traffic to your local server.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And voila 🎉, you can now open the generated url on your mobile browser to see your app on your phone. And, it even supports hot reload.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Pro tip :&lt;/strong&gt;  Instead of typing that long url on your phone, you can generate a QR code by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx qrcode &lt;span class="s2"&gt;"&amp;lt;generated-url-here&amp;gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>tutorial</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
