Tunnelling Nodejs Applications with "ngrok"

Tunnelling Nodejs Applications with "ngrok"

Intro

ngrok is a network utility application that tunnels a local webserver to the internet where it can be publicly accessed. ngrok creates two subdomains - one http and the other https - through which your local server can be accessed by anyone, anywhere in the world.

Why ngrok ?

When developing Nodejs applications, ngrok enables you expose your application to the internet without going through the stress of hosting/deployment. This is useful if you want a client to check the current state of your application or you are building an API and you want to quickly test it by making requests from a client application.

Installation

Since this article is all about using ngrok with node, we would just install it directly using npm .

npm i ngrok

This downloads the ngrok binary and also the node module for interacting with it.

Usage

We are going to look at two way of using ngrok. Firstly, we could decide to call it in our node application. This would look like this:

const express = require('express')
const bodyParser = require('body-parser')
const appSetup = require('./appSetup.js')
const dotenv = require('dotenv')
const ngrok = require('ngrok')
const router = require('./router')
dotenv.config()
const app = new express()

async () => await appSetup(app)

const port  = process.env.PORT || 3200
app.use('/', router)

app.listen(port, (err) => {
    if(err) return console.log(err.message)
    console.log(`App is running on port ${port}`)
    ngrok.connect()
        .then(url => console.log(url))
        .catch(err => console.log(err.message))
})

As you can see, after our app is running locally, we call ngrok.connect() which returns a promise, that resolves with the url of the subdomain that our application is being tunneled to. Any request to that url would be forwarded back to our local server, as long as our server is still up and running. ngrok provides a nice web interface that is available on localhost:4040, which is useful for inspecting requests made to the application.

The second method involves using ngrok directly. Assuming our application is running on port 3000, running this on the terminal would expose our application to the internet:

ngrok http 3000

Two randomly generated urls - one http and the other https - would be displayed through which the application can be accessed from the internet

API

Here, we would explore a couple of things about the ngrok API.

Custom subdomain

The url generated for your application changes whenever you rerun ngrok and this can sometimes be frustrating. If you are on a paid plan, you could use a custom subdomain by passing the -subdomain option

ngrok http -subdomain aleeycreative 5000

The url for accessing your application would now be aleeycreative.ngrok.com.

Sharing a directory

Ngrok can also expose a local directory on the internet. It can be secured by passing the -auth option:

ngrok http -auth "user:pass" file:///path/to/directory

Serving a local https server

If we have a local https server running on port 3000, sharing it with ngrok is very easy:

ngrok http https://localhost:3000

Disconnecting from node

Normally, the connection is destroyed when the node process terminates. However running either of the command below would terminate the connection early:

async() => await ngrok.disconnect()
async() => await ngrok.kill()

Conclusion

Thanks for reading !!! I hope you learnt a thing or two about ngrok from this post. Bye for now !!

Further Reading