當我們在本機進行開發時,有時候會想要用一些實體的外部裝置進行測試,來看看實際的結果如何,譬如用手機測試網站畫面等等。這時就可透過 ngrok 來將我們的服務直接放到線上,使外部裝置可以連進本機上的這個 localhost 服務


Setp 1: 註冊並下載 ngrok Link to heading

點選 Sign up 註冊

image

註冊完成後,依照 ngrok 網站上的步驟操作,執行下載的 ngrok.exe

image


Setp 2: ngork 設定 Link to heading

依照 ngrok 網站上的步驟操作,在 ngrok.exe 開啟的 Command Line 畫面上執行 add-authtoken 指令:

ngrok config add-authtoken XXXXXXXXXXX

image

image


Setp 3: 執行 ngork,讓外部裝置來使用本地服務 Link to heading

執行 ngork,若你使用的是 http,則執行: ngrok http http://localhost:8080

image

執行後會顯示如下:

image

Forwarding 後面的 url 就是讓外部裝置可以直接開啟本機服務的 url
Ctrl+C 即可結束 ngork 服務


若是 https,則執行:

ngrok http https://localhost:443 --host-header=localhost:443

image


P.S. 若有自行設定的 port,則也需做調整



參考來源: Link to heading

  1. ngrok