Cloudflare quick tunnels Node.js manager. Expose localhost to the internet with programmatic cloudflared process management, URL extraction, and automatic rotation.
cloudflaredjs is a Node.js wrapper for Cloudflare quick tunnels. It acts as an open source ngrok alternative that runs on Cloudflare edge infrastructure.
Cloudflare quick tunnels expose local web applications to the internet without an account, custom domain, or configuration file. Running cloudflared tunnel --url http://localhost:3000 generates an ephemeral trycloudflare.com URL. When the tunnel connection drops, restarting cloudflared generates a different random URL.
cloudflaredjs manages this lifecycle in JavaScript. The module starts the child process, extracts the public URL from the process output, polls the connection, and restarts the tunnel when Cloudflare disconnects. You can capture the new URL in a callback and update your database, DNS records, or webhook endpoints.
cloudflaredjs coordinates four mechanisms:
- Process spawning. The library calls
child_process.spawnto runcloudflared tunnel --url http://localhost:<port>. On POSIX systems, it spawns detached child processes in an isolated process group. On Windows, it spawns standard child processes. - Output parsing. The module reads stdout and stderr lines through Node.js
readline. It matches URLs ending withtrycloudflare.comusing regular expressions. If 11 lines pass without finding a URL, the startup promise rejects and the process terminates. - Health monitoring and rotation. When
autoFaultDetectionAndUpdateis true, an interval timer polls the public tunnel URL using globalfetcheverydelaymilliseconds. If Cloudflare returns an HTTP 502 or 503 gateway error, the library kills the process, starts a fresh tunnel, and passes the new URL tosuccessCallback. If consecutive failures exceedafterFaultRetries, the loop stops and callsfaultCallback. - Clean termination. The
killChildmethod stops the active tunnel and clears all active timers. On POSIX systems, it sendsSIGTERMto the process group (-pid). On Windows, it callstaskkill /PID <pid> /T /F. The library also registers listeners forSIGINT,SIGTERM,exit,uncaughtException, andunhandledRejectionto kill child processes before Node.js exits.
- Node.js 18 or higher. The health check loop uses the native global
fetchAPI. - The
cloudflaredexecutable installed and available in your systemPATH. Refer to Cloudflare documentation for official package manager or binary download instructions.
Install the package directly from GitHub:
npm install github:devsanthoshmk/cloudflaredjsStart a single quick tunnel and obtain the public URL:
import { createTunnel } from 'cloudflaredjs';
const tunnel = createTunnel();
try {
const url = await tunnel.startCloudflared({
port: 3000,
verbose: true,
});
console.log('Tunnel public URL:', url);
} catch (error) {
console.error('Failed to create tunnel:', error);
} finally {
// Stop the tunnel when finished:
// tunnel.killChild();
}Monitor the tunnel and receive new URLs automatically when Cloudflare drops the connection:
import { createTunnel } from 'cloudflaredjs';
const tunnel = createTunnel();
const url = await tunnel.startCloudflared({
port: 3000,
verbose: true,
autoFaultDetectionAndUpdate: true,
delay: 8000,
afterFaultRetries: 10,
successCallback: (newUrl) => {
console.log('Tunnel rotated to new URL:', newUrl);
// Write newUrl to your database, key-value store, or webhook destination
},
faultCallback: () => {
console.error('Tunnel failed permanently after exhausting retries.');
},
});
console.log('Initial tunnel URL:', url);
process.on('SIGINT', () => {
tunnel.killChild();
process.exit();
});When sharing dev environments or testing webhooks, quick tunnel URLs change across restarts. To maintain a stable entry point, host a static page on Cloudflare Pages or Vercel that renders the dynamic URL inside an <iframe>:
- The backend service runs
cloudflaredjsand saves every new URL fromsuccessCallbackto an API or database. - The static frontend queries that API on page load and assigns the target URL to the iframe
src. - Visitors use the permanent frontend domain while viewing live updates from your local server.
The startCloudflared function accepts the following options:
| Option | Type | Default | Description |
|---|---|---|---|
port |
number | None (required) | Local port number forwarded through the tunnel. |
verbose |
boolean | false |
Prints runtime messages to the console and writes stdout and stderr logs to ./cloudflaredjs.<port>.logs.txt. |
autoFaultDetectionAndUpdate |
boolean | false |
Enables the background health check loop and restarts the tunnel on HTTP 502 or 503 errors. |
successCallback |
function | () => {} |
Receives the new tunnel URL string after an automatic restart. |
faultCallback |
function | None | Called when consecutive restart failures exceed afterFaultRetries. Required when autoFaultDetectionAndUpdate is true. |
delay |
number | 8000 |
Interval in milliseconds between health check requests. |
afterFaultRetries |
number | 10 |
Maximum number of consecutive health check failures before stopping retries. |
createTunnel() returns an object with two functions:
startCloudflared(options). Spawns thecloudflaredprocess and returns aPromise<string>resolving to thetrycloudflare.comURL.killChild(). Stops the running child process and clears the health check timer.
Quick tunnels run on shared Cloudflare infrastructure under Cloudflare terms of service:
- Run no more than 3 to 4 simultaneous tunnels per machine.
- Avoid rapid restart loops. Frequent reconnections trigger temporary Cloudflare IP blocks that last 10 to 15 minutes.
- Quick tunnels target development, staging previews, and webhook testing. Do not use quick tunnels for permanent production hosting.
MIT