Exposing localhost to the public Internet
In this article, we'll see how developers can expose their localhost development server to the public Internet.
Suppose you you are working on a project on your development machine. Once the project is complete and works as expected, you may be eager to share it!
Hosting it on a web server needs a domain name, web hosting, and other things.
Some may suggest port forwarding. Port forwarding redirects data meant for a computer's IP/port to an other IP and/or port.
However, you most certainly want to access localhost from the Internet without spending much time configuring port forwarding or a web server.
Tunnels come in handy here; you can use free tools like ngrok or cloudflare tunnels to establish a secure but temporary connection between your localhost and the internet.
Some popular services include:
- Cloudflare tunnel
There are also a number of paid options, like pagekite, Forward and Burrow.io.
Exposing localhost with ngrok
ngrok is most likely the solution that should be used for this. Although it is not technically free and is more accurately described as freemium, the free tier does not cost any money and is enough for the majority of development needs.
You will be required to create an account before obtaining an authentication token, which will provide you access to a wide variety of useful functions. The introductory documentation provided by ngrok is very excellent, and it answers the majority of the frequently asked questions and walks you through the activities that you would wish to complete. Installers are provided for Windows, Mac, and a variety of Linux distributions.
If your application is running on port 4000, you can use the following command to create a HTTP tunnel:
ngrok http 3000
Because ngrok has a large number of features, it is recommended that you explore the documentation in order to get familiar with the available options. The commercial features of ngrok include the ability to utilize custom or reserved domains (instead of having a random name each time you run it), the ability to reserve TCP addresses, and IP whitelisting, among other capabilities.
Exposing localhost with localtunnel
Localtunnel is an open-source project that performs the exact same function as the two services that were discussed before. It is necessary for your system to already have Node.js installed in order for you to be able to install it using npm using the following command:
npm install -g localtunnel
After that, you will obtain access to the lt command, and you will be able to start your HTTP tunnel using the following command:
lt --port 4000
Make sure to change the command to correspond with the port on which your web application is operating.
You will get a publicly accessible URL and for the most part, that would be enough for you to set up an integration with a service that supports webhooks.
You will be provided with a URL that is open to the public.
What’s interesting about Localtunnel is that there is also a repository you can clone to set up your own localtunnel server, and hence use your own custom domain. This approach requires you have control of a server where you can set up DNS entries, as well as handle incoming TCP connections for any non-root TCP port.
Exposing localhost with telebit
Because of all the many reasons, I find Telebit to be rather amazing. In addition to forwarding ports, you can use Telebit to exchange files and configure it to work like a virtual private network (VPN).
After installing telebit, you can use it as follows:
telebit http 4000 Forwarding https://jondoe.telebit.io => localhost:4000
Exposing localhost with localhost.run
localhost.run exposes your local web server to the web with a public URL.
You’ll need to run the following command:
ssh -R 80:localhost:4000 ssh.localhost.run
Also, make sure to change the command to correspond with the port on which your web application is operating.
You’ll get an output with the RSA key fingerprint of the localhost.run server when you make a connection for the first time, and you’ll need to agree to continue.
Exposing localhost with cloudflare tunnels
The Cloudflare tunnel service operates a lightweight process in your machine called cloudflared, which is responsible for establishing outgoing connections called tunnels between your origin web server and the Cloudflare servers. In everyday language, this means that you may use Cloudflare Tunnel to provide remote access to services that are operating on your local host.
Just download and install the Cloudflare Tunnel CLI tool.
By executing the following command, you may ensure that cloudflared was properly installed on your system:
Run the tunnel command and point it to the host port you want to serve and it will spit out a URL. With a local development server running, a new Cloudflare Tunnel can be instantiated by running
cloudflared tunnel in a new command line window, passing in the
--url flag with your localhost URL and port.
cloudflared will output logs to your command line, including a banner with a tunnel URL:
When you run the tunnel command and tell it the host port you want to serve, it will create a URL for you automatically.
If you already have a local development server up and running, you may initiate the establishment of a new Cloudflare Tunnel by opening a new command-line interface, typing cloudflared tunnel, and using the —url option along with the localhost URL and port in your command:
cloudflared tunnel --url http://localhost:4000
Here we create a tunnel that points to the http://localhost:4000.
The cloudflared process will produce logs that will be printed to your command line, including a banner that contains a tunnel URL.
When you go to this tunnel URL in a web browser, you will see that the application is operational. Requests will be securely forwarded across Cloudflare's edge network, through the tunnel that is operating on your development machine, and they will be directed to localhost:3000.
You can then test services that requires secure connection or show the demo to your friends, coworkers or clients!
For additional information on setups and configuration, check out Cloudflare docs.
There are so many ways to expose localhost to the web. We recommend checking out their websites to determine which one meets your requirements.
This is a handy feature for developers that you may use to easily show your running web applications to co-workers, friends and clients straight from your development machine via Internet.
Please share how to access localhost from the Internet with your friends and coworkers.
✋If you have any questions about this article, ask them in our GitHub Discussions 👈 community. You can also