Using ngrok as an external process

I don’t know why it took me so long to figure this out but it turns out that the ngrok client has an api that you can access locally. That means you can run ngrok external to your process and then call to it to get the public tunnel information. This is very handy as ngrok doesn’t really like starting and stopping as rapidly as you may want while developing a server.

This means rather than using the ngrok package on npm for your local development, look towards docker hub:

I added an entry to my docker-compose.yml file like so:

version: '3'
    image: wernight/ngrok
      - 4040:4040
      NGROK_PORT: 4000
      NGROK_PROTOCOL: http

Next in your code, after staring the containers, simply call the service from your application code:

const { status, data } = await axios.get('http://localhost:4040/api/tunnels')
const { tunnels } = data
const { public_url } = tunnels.find(t => t.proto === 'https')
this.publicEndpoint = public_url

The ngrok tunnel will stay alive as your server restarts over and over.

Author: justinmchase

I'm a Software Developer from Minnesota.

Leave a Reply

%d bloggers like this: