LoginSignup
1
2

More than 1 year has passed since last update.

ngrok | グローバルなURLからローカルサーバーを参照する ( Slack API チュートリアルより )

Last updated at Posted at 2017-05-12

ngrok をダウンロード

https://ngrok.com/ より

解凍して、パスの通っているディレクトリに移動させる

例:

$ unzip ~/Downloads/ngrok-stable-darwin-amd64.zip
$ mv ngrok /usr/local/bin

ngrok を起動

$ ngrok http 4390

image

画面に表示されたURLにアクセスする。

( この場合は https://ec6b5635.ngrok.io )

ここではまだエラーが出る。

image

ローカルサーバーを立てる

やり方はなんでも良いが、ここでは node を使ってみる。

node のインストール

$ brew install node

サーバー起動用ファイルを作成

indes.js
// First we need to import the HTTP module. This module contains all the logic for dealing with HTTP requests.
var http = require('http');

// We define the port we want to listen to. Logically this has to be the same port than we specified on ngrok.
const PORT=4390;

// We create a function which handles any requests and sends a simple response
function handleRequest(request, response){
  response.end('Ngrok is working! -  Path Hit: ' + request.url);
}

// We create the web server object calling the createServer function. Passing our request function onto createServer guarantees the function is called once for every HTTP request that's made against the server
var server = http.createServer(handleRequest);

// Finally we start the server
server.listen(PORT, function(){
  // Callback triggered when server is successfully listening. Hurray!
  console.log("Server listening on: http://localhost:%s", PORT);
});

ローカルサーバーを起動する

$ sudo node index.js

Server listening on: http://localhost:4390

再度 グローバルURLにアクセスする

( この場合は https://ec6b5635.ngrok.io )

動いているようだ。

image

ログ

コンソールにレスポンスが記録されている

image

ローカルのウェブインターフェイス

こんな感じで記録されている。

( この場合は http://127.0.0.1:4041 )

image

その他

もちろん Rails とかでも出来る!(ポートを合わせよう)

image

環境

  • Mac OS X El Capitan 10.11.6

参考

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2