本地端口转发
通过本地端口转发,我们可以将本地的 http(s) 服务分享到任何地方,可以用于临时的调试
方式一 untun
unjs 提供的隧道工具 untun
$ npm i -g untun
$ untun tunnel http://localhost:5173◐ Starting cloudflared tunnel to http://localhost:5173
ℹ Waiting for tunnel URL...
✔ Tunnel ready at https://caroline-screen-peer-passes.trycloudflare.com其他用户就可以通过 https://unjs-is-awesome.trycloudflare.com 访问到本地上的 http://localhost:8080 服务了,而且如果 8080 服务依赖内网其他服务,也不需要挂 VPN 之类的,访问的流量通过隧道直接进入内网
一种临时性的方案,调试非常方便
方式二 VsCode Port Forwarding
另一种方案是使用 vscode 提供的服务,这种方式还提供了可选的安全验证,例如两端登录了同一 Github/Macrosoft 账户才能访问:
分享静态资源
结合 vercel 提供的 vercel/serve 工具,可以将本地的静态资源通过 http(s) 分发到一切地方!
安装
npm i -g serve使用
将本地资源通过服务代理,再通过 untun 往外进行分发
$ serve ./ -l 7777 & untun tunnel http://localhost:7777◐ Starting cloudflared tunnel to http://localhost:7777
ℹ Waiting for tunnel URL...
┌─────────────────────────────────────────┐
│ │
│ Serving! │
│ │
│ - Local: http://localhost:7777 │
│ - Network: http://192.168.1.8:7777 │
│ │
│ Copied local address to clipboard! │
│ │
└─────────────────────────────────────────┘
✔ Tunnel ready at https://wings-inches-kansas-enforcement.trycloudflare.com执行完毕后,其他用户就可以通过 https://spreading-algorithm-lose-meal.trycloudflare.com 访问下载到本地的静态资源了。serve 服务还提供了资源的目录列表,非常便于浏览!