Ngrok is one such tool that is very crucial for developers since it allows them to easily make their development server accessible on the internet. It is a great tool for testing webhooks, sharing your local work with stakeholders and exposing servers to the public. This article will demonstrate how to go about port forwarding with Ngrok.
Ngrok is a tunneling service that will securely tunnel from a public endpoint (internet) to your local machine. It allows you to expose local servers or applications to the internet. Ngrok provides HTTP(S) or TCP tunnels and lets you map your local ports to a URL that can be publicly accessed. It is extremely useful in testing webhooks, your local development, simulating live environments and even for sharing all without having to deploy to a remote server.
Here are the steps you will have to take in order to set up Ngrok:
You will first have to download and install Ngrok. Go to Ngrok’s website and choose the version that corresponds to your OS.
On Linux/Mac:
Run the following commands to download Ngrok:
wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
# Unzip the downloaded file
unzip ngrok-stable-linux-amd64.zip
# Move the Ngrok binary to a folder in your system PATH (optional)
sudo mv ngrok /usr/local/bin
On Windows:
For Windows, downloading the .zip file, unzipping it, and placing the Ngrok binary into any folder of your preference is sufficient. Add the folder to the system’s PATH variable for easier access from the command line.
In order to connect your account and local machine, you must authenticate Ngrok and follow the steps stated above. If you do not have an Ngrok account, it is advisable to create one from their website. After logging into your profile, you will find the authentication token on the Ngrok dashboard.
To authenticate, execute the command below:
ngrok authtoken <your-auth-token>
This will enable you to use Ngrok’s advanced features like custom subdomains and reserved domains.
Ngrok can be used to expose a web application by connecting it to a local server. If, for example, you are making a web project, you can start a simple HTTP server on port 3000. You must have a local server running to use ngrok.
For users utilizing Node.js, the command below will allow you to open your server:
# Within the folder containing your project
npm install
npm start
Ensure your server is running on a port like 3000 or 8080.
Having set up a local server, you can now use Ngrok to facilitate its exposure. Start by running the following command, with the necessary port number replacing 3000:
ngrok http 3000
Ngrok will provide you with a public URL which usually follows the sequence https://1234abcd.ngrok.io. That links directly to your local server. The URL can now be opened from any device that has internet access. Anywhere in the world, you can run and get access.
Ngrok offers a public URL available to visit through any internet browser. Public traffic will reach your local server through the Ngrok URL. Through Ngrok you can gain remote access to your local application which helps you share projects externally.
Strip and GitHub users can benefit from Ngrok as it helps them test their webhooks. The Ngrok URL receives HTTP requests from these services which enables you to test your webhook handling process.
Whenever a tunnel is started, Ngrok, by default, creates a random subdomain on each occasion. However, for a custom subdomain or reserved domain, you would have to sign up for a paid Ngrok plan which allows you to create a custom subdomain. For example:
ngrok http -subdomain=myproject 3000
This command would give you a permanent, easy-to-remember URL like https://myproject.ngrok.io.
Unlocking New Features of Ngrok
Ngrok offers new features that can further enhance your productivity:
ngrok tcp 3306
This will expose your local MySQL database or other TCP-based service.
ngrok http -auth="username:password" 3000
This adds a layer of protection to your publicly exposed server.
Ngrok highly enhances application testing and sharing by providing exceptional means to expose a local server to the internet. It is very useful while working on webhooks, developing mobile applications, or even when you need to present a project to a potential client, this makes Ngrok an indispensable service for developers in contemporary times.
This guide will provide you tools on how to make use of ngrok's port forwarding feature so that it can improve your workflow in development.
Ready to transform your business with our technology solutions? Contact Us today to Leverage Our DevOps Expertise.