如何从互联网访问本地主机?
从互联网访问本地主机
ngrok.com
localtunnel.me
假设你正在做一个业余项目,或者在本地构建一些项目来学习一些概念。一旦你完成了这个项目,并且它按预期运行,你可能会很兴奋地想和你的朋友和同事分享它。
但是,您不想陷入在网络服务器上托管它的痛苦,因为这涉及购买域名、托管空间和各种其他配置。
现在,有些人可能会建议使用端口转发选项。对于那些不了解端口转发的人来说,它是一个拦截发往计算机 IP/端口组合的数据流量并将其重定向到其他 IP 和/或端口的过程。
我确信您希望从互联网访问本地主机,而又不想麻烦地进行端口转发并将应用程序托管在 Web 服务器上。是吗?
从互联网访问本地主机
您可以使用一些免费的服务从互联网访问 localhost。我们将在这篇博文中讨论其中一些服务:
我们可以使用其中任何一种来实现最终目标。我用过 ngrok,发现它设置和使用都很简单。实际上,根本不需要设置。
如果您是 Windows 用户,您只需从 ngrok.com 下载二进制文件并运行它。
ngrok.com
ngrok(发音为“en-grok”)为您提供在本地主机上运行的 Web 应用程序的公共 URL。
它是一种反向代理软件,可以建立从公共端点到本地运行的服务的安全连接。
ngrok 的使用方法有两种:
- 从官方网站下载软件
- 使用 npm 安装
访问官方文档以获取更多信息。
下载及使用步骤:
- 访问 ngrok.com
- 下载 ngrok
- 解压 zip 文件并双击 ngrok.exe
一旦您设置了 ngrok 并执行它,将会出现如下所示的命令提示符:
localtunnel.me
Localtunnel 将为您分配一个唯一的可公开访问的 URL,该 URL 将所有请求代理到您本地运行的网络服务器。
为了使用 localtunnel,您需要在您的机器上安装 nodejs。
全局安装 localtunnel:
npm install -g localtunnel
要在端口 80 上启动 Web 服务器:
lt --port 8000
```
To use port 3000:
```
lt --port 3000
```
#localhost.run
localhost.run is a client-less tool to instantly make a locally running application available on an internet-accessible URL. It doesn’t require us to install anything on our machine.
Simply, you can go to command prompt and execute the command given below:
ssh -R 80:localhost:8080 localhost.运行
Note: To open a command prompt, you can press Windows + R keys (⊞ Win + R), then type “cmd” and click on Enter.
If your application is running on local port 3000, then use this command:
ssh -R 80:localhost:3000 localhost.运行

With the help of these three services, you can access localhost from the internet. You may figure out the one that works best for you and enjoy sharing your publicly accessible URL among your friends.
This is my localhost reactjs app that is now exposed over the internet using ngrok service.

#Closing Notes
This is a great utility for all the devs out there. Now, whatever web apps you build on your local system can be shared across with such ease.
Now, you know to access localhost from the internet, please share it with your friends and colleagues to educate them as well.
You may check our post on 👉 [Getting started with Deno](https://dotnetcrunch.in/getting-started-with-deno/)
Please visit our YouTube channel 👉 [DotNetCrunch](https://www.youtube.com/channel/UCXjb2EQf9pGQd6LlF1m6d_w)
Happy Learning!