How to Use the Command 'lt' (with examples)
Localtunnel is a convenient tool that allows developers to expose their localhost server to the internet. This makes it easy to share applications in development for testing and collaboration without deploying the app or adjusting firewall settings. Whether you’re looking to show your app to a client or allow a colleague to review your project, Localtunnel can create a secure tunnel from your local machine to a public endpoint.
Start Tunnel from a Specific Port
Code:
lt --port 8000
Motivation:
You might be developing a web application on your local machine, and you desire to share it quickly with a teammate or test it on another device. By exposing the localhost on a specific port, you can easily grant access to the server running on that port without complicated setups.
Explanation:
--port 8000
: This argument specifies which port on your localhost you want to expose via the tunnel. Here, it assumes your application is running on port 8000.
Example Output:
Upon executing this command, you will receive a URL like https://unique-url.loca.lt
, which directs external traffic to your localhost server running on port 8000.
Specify the Upstream Server Doing the Forwarding
Code:
lt --port 8000 --host host
Motivation:
In some scenarios, you might want to route traffic through a specific server. For instance, if the default Localtunnel server is experiencing heavy traffic or you’re required to use a particular server for compliance or geographical reasons.
Explanation:
--port 8000
: Specifies the port number that your local application is using.--host host
: This option sets the upstream server that will handle the forwarding process. By specifying a custom host, you control which server processes the tunneling request, although typically, the default server is used.
Example Output:
Executing this command provides a URL like https://custom-host-url.loca.lt
in which the traffic is routed through your specified server.
Request a Specific Subdomain
Code:
lt --port 8000 --subdomain subdomain
Motivation:
If you’re demonstrating a prototype for branding or clarity reasons, it can be beneficial to request a specific subdomain that aligns with the client or project name, rather than a randomly generated one. This can simplify communications about accessing the demo.
Explanation:
--port 8000
: Indicates the port on your local server you aim to expose.--subdomain subdomain
: Requests a specific subdomain for the tunnel URL, rather than defaulting to a randomly generated one. This name should be easy to remember and relevant to the application being tested.
Example Output:
Running this will yield a URL like https://custom-subdomain.loca.lt
, making it intuitive to recognize and share.
Print Basic Request Info
Code:
lt --port 8000 --print-requests
Motivation:
Developers often need insights into incoming requests during development. This option allows you to view basic request information, aiding in debugging and logging.
Explanation:
--port 8000
: Targets the port your application is currently listening on.--print-requests
: This argument triggers the local server to log each incoming request’s basic information like HTTP method type, endpoint, headers, and data payload.
Example Output:
When activated, every incoming request detail, including its headers and body, will appear in your terminal, providing real-time insights.
Open the Tunnel URL in the Default Web Browser
Code:
lt --port 8000 --open
Motivation:
Time-saving is crucial during development. Automatically opening the tunnel URL in your default browser saves you the extra step and allows you or your team to immediately start testing the exposed application.
Explanation:
--port 8000
: Identifies the port number you are tunneling through to the external network.--open
: This flag automatically launches the tunnel’s URL in the default web browser as soon as the tunnel establishes a connection.
Example Output:
Once the tunnel initializes, your default browser opens to display the application running on your localhost, providing instant access without manual URL navigation.
Conclusion
Localtunnel provides developers a straightforward and effective way to share applications in development directly from their local machines. By utilizing various options, users have flexibility in terms of ports, servers, subdomains, and convenience features like automatic browser opening or request logging. These capabilities make Localtunnel a must-use tool in any developer’s toolkit when ease of access and collaboration are priorities during development cycles.