I have a server that runs via Node on my machine. It runs when my pc starts up under a custom subdomain using lt --subdomain mydomain --port 3000
I noticed that it started to randomly crash after about 6 hours. I put this down to either the node server or localtunnel restarting or my internet being poor quality.
When i rerun the above localtunnel command it says the subdomain is taken (gives a random subdomain). How do I make the localtunnel close the tunnel correctly to allow for my subdomain to be reused?
I've managed to make a class that redigs/restarts the tunnel if a connection error occurs. It is not a fix to the problem, but it prevents it from occuring. I've been using this as a solution for a year now and haven't had a single problem with it, so I hope it works for you too.
easy-tunnel.js
const { exec } = require("child_process")
const colours = require("colors")
const base_host = 'http://localtunnel.me'
class EasyTunnel {
/**
*
* @param {number} port
* @param {string} subdomain
* @param {string} [host] defaults to 'http://localtunnel.me'
*/
constructor(port, subdomain, host) {
this.port = port
this.subdomain = subdomain
this.host = host ? host : base_host
}
/**
* Start the EasyTunnel
* @param {string} [status] used so redigging does not show initial dig message
*/
start(status) {
if (status != 'redig') console.log(`> Dug a tunnel for ${this.subdomain.yellow} on port ${this.port.toString().cyan} at ${this.host.green}`)
exec(`lt --subdomain ${this.subdomain} --port ${this.port} --host ${this.host}`, async (err, out) => {
if (err.toString().includes('connection refused')) {
this.start("redig")
console.log("> Redigging tunnel")
}
})
}
}
module.exports = EasyTunnel
main-app.js
var express = require("express")
var easyTunnel = require("./imports/easy-tunnel")
var port = 3000
var localtunnel = new easyTunnel(port, 'website-name')
var app = express();
app.get("/", (req, res) => {
res.send(":)")
}
app.listen(port, () => localtunnel.start());