How to Expose Your Local Host to the Internet

Ifeanyi Idiaye
6 min readAug 3, 2024

--

Local host

In this post, I will show you how to easily expose your application running on local host to the internet such that it can be accessed by others through a unique URL.

Why Expose an App to the Internet?

There are a number of reasons why it becomes necessary to expose your locally running application to the internet. Here are some of those reasons:

  • Remote Access: Allows users to the app from anywhere, not just on the local network.
  • Collaboration: Facilitates collaboration among team members who are working remotely or from different locations.
  • Client Demonstrations: Enables you to demonstrate the app to clients or stakeholders who are not physically present.
  • User Testing: Allows a broader audience to test the app, providing valuable feedback and helping to identify issues that may not appear in local environment.
  • Data Collection and Analytics: Facilitates real-time data collection and analytics from a geographically diverse user base.

The reasons given above are very vital. However, it is important to bear in mind that this post is not about deploying an application, but about exposing it to the internet through a URL that can be accessed by anyone who has access to it. That is why we use the term expose rather than deploy.

The difference between the two is that when an app is simply exposed to the internet, it is still running locally on your computer but it can be accessed remotely through a URL, with your computer acting as the server. However, when an app is deployed, it is no longer running locally on your computer but on a remote server from which it can be accessed through a URL. With that clarification, let us now demonstrate how to expose an app to the internet.

Enter Ngrok

For this example, we will make use of a tool called Ngrok. Ngrok is a tool that provides secure tunnels to localhost, allowing developers to expose a local server to the internet. It is commonly used for testing webhooks, sharing local developments, and other purposes that require external access to a local server.

To get started, you will need to download and install Ngrok on your computer. To do so, you will have to create an account on their website and download the application that is compatible with your computer’s operating system. You will see versions for Windows, Mac, Linux, etc. in your dashboard.

You can also use Ngrok’s SDKs in any programming language of your choice to achieve the same goal.

After downloading the application on your computer, start the installation and follow the installation guide. When that is done, run the following command in your command prompt:

ngrok

If all goes well, you should see something like this in your command prompt:

Ngrok command line

This indicates that Ngrok has been successfully installed on your computer and added to the system path of your computer. However, if you get an error that says that “ngrok” is not recognized as a command on your computer, it means it has not been added to your computer’s system path.

To add it to your computer’s system path, follow the steps below:

  1. Open Environment Variables:
  • Press Win + X and select System.
  • Click on Advanced system settings.
  • In the System Properties window, click on the Environment Variables button.

2. Edit the PATH Variable:

  • In the Environment Variables window, find the Path variable under the System variables section and select it.
  • Click on the Edit button.

3. Add Ngrok Directory to PATH:

  • In the Edit Environment Variable window, click on New.
  • Enter the path to the directory where you extracted Ngrok or where the download files are (e.g., C:\ngrok).
  • Click OK to close the windows and save the changes.

After this is done, try typing ngrok again in the command prompt and press enter to see if you will get the same kind of message displayed in the command prompt above. If you do, then congratulations! You can now call Ngrok from the command line.

Expose Localhost to the Internet

It is now time to expose your locally running server to the internet using the power and capability of Ngrok. All it takes is just one line of code to achieve this:

ngrok http <your localhost address>

To demonstrate this, I have created a simple web application that generates random quotes whenever a user clicks the button. If you would like to use this application to follow along, then you will need to clone the GitHub repository here. Also, since the application is served by Node.js, you will need to have Node.js installed on your computer to be able to start the local server.

Nevertheless, you can use any application running locally on your computer on any server framework. What is important is to be able to get the local host address of the server.

Assuming you have met all the necessary conditions, you can now navigate to that repository on your computer by running the following in your terminal:

cd LocalHost

After that is done, you want to start the local server by running:

node app.js

You should see your local host URL that looks something like this:

http://127.0.0.1:4000/

Next, copy that URL and paste it in your browser, and you should see the app running

Random quotes generator app

Great! Now, to expose this application running on your local server to the internet using Ngrok, you will need to open a second terminal or command prompt and run the line of code below:

ngrok http http://127.0.0.1:4000/

Note the double http (it is not a typo). In your second command prompt, you will see something like this:

Ngrok CLI

Now, copy the randomly generated URL, highlighted below, and paste it in your browser:

Ngrok CLI

You will be asked whether you want to visit the website. Click “visit”, and you should now have access to the web app.

Random quotes generator GIF

With this, your app is now live on the internet. Your local host has been successfully exposed to the internet, and you can share that Ngrok-generated link with anyone so that they can also access your application.

Remember, though, that this is an ephemeral setup. Users can only access the application as long as the server is running on your computer. Once you stop the server, they can no longer access your app. You will need to deploy your application if you want it to run on a remote server and be persisted.

I hope you found this post helpful. If you did, please drop a clap below👏 and share this post with others. 😃

Follow me on GitHub: https://github.com/Ifeanyi55

Follow me on Medium: Ifeanyi Idiaye

Follow me on X: @Ifeanyidiaye

Connect with me on LinkedIn: https://www.linkedin.com/in/ifeanyi-idiaye-09523abb/

You can also read some of my other posts on Medium and Dev Genius:

--

--

Ifeanyi Idiaye

I am a data scientist who is passionate about AI and building AI-driven applications. I am also a software developer and writer for Dev Genius.