Get a public URL to Localhost — The easy way
If you’re a web developer or an API developer, then you should have wondered how to expose your localhost to the outside world. Some common scenarios are testing chatbots, testing browser notifications, testing your website on a mobile device, and you name it.
You may have tried techniques like remote port forwarding. But they have drawbacks like cost and security concerns. There’re lots of free services to do this for you. In this article, I’m explaining ngrok. Which I found pretty convenient to use, and it’s free.
Go to their website. https://ngrok.com/
Then create a free account, next go to the getting started page. It has all the steps you need to follow.
Step 1 and 2 — Download and install ngrok in your PC.
Step 3 — Connect your account to ngrok by running following command
./ngrok authtoken <<authtoken>>
you have to replace <<authtoken>> with your auth token, but you can simply find the full command with your token in the getting started page.
That’s all set up, and then you’re good to go 😮
Let’s assume you’re running an Express REST API in port 3003, and you want to make it publicly accessible. Then all you have to do is running following command.
./ngrok http 3003
You have to replace 3003 with the actual port your service is running. Then you can see the following screen,
In there, I have marked two URLs. First one is the HTTP public URL, and the second one is the HTTPS public URL to your service. Simply copy and paste the URL you need, and that’s it. Your service in localhost now has a public URL 😎
They have provided a simple UI to monitor the network calls, which is very helpful. You can access it using the following URL
http://localhost:4040/
But keep one thing in your mind. This public URL is changing every time you restart ngrok. If you want to stop it, then you have to go for a paid version.
If you require to password protect your URL, that can be done by modifying the above command to following,
./ngrok http -auth="username:password" 3003
They provide another cool feature. You can host a fileserver with a public URL to host files in your local PC very easily. Jut run following command then, you would get your public fileserver (replace <<directory>> with the directory you need to share).
./ngrok http -auth="user:password" file://<<directory>>
Apart from what I mentioned here, ngrok comes up with lots of other cool features, check out their documentation page to find all of them.
Not only ngrok, but there’re several other free services available to do the same. Localhost.run is one of my favourites
Localhost.run
localhot.run doesn’t require you to install anything or create an account. Just running a simple command like following in the terminal would do the job,
ssh -R 80:localhost:3003 ssh.localhost.run
This one is proper if you have a quick demo or a simple thing to do. The main drawback in this is, it doesn’t come up with all the fancy features ngrok offers and lack of configurations available.
You may find some other free and paid services that would do the same as ngrok. If you found a tool more interesting, feel free to share it by a comment.
Thank you for reading. Happy Coding 💻