Skip to content

Instantly share code, notes, and snippets.

@NeuronButter
Last active July 27, 2023 17:16
Show Gist options
  • Save NeuronButter/821a7cb890c2d275ab39becea028a961 to your computer and use it in GitHub Desktop.
Save NeuronButter/821a7cb890c2d275ab39becea028a961 to your computer and use it in GitHub Desktop.
Programmatically use cloudflared Quick Tunnels
// Original answer here https://community.cloudflare.com/t/quick-tunnel-from-nodejs/336868/3
const express = require('express');
const { spawn } = require('child_process');
let port = 8080;
// Express
let app = express();
app.get('/', (req, res) => res.send('Hi'));
app.listen(port);
// Cloudflared
const cloudflared = spawn('cloudflared', ['tunnel', '--url', `http://localhost:${port}`]);
cloudflared.stderr.on('data', (data) => {
let output = data.toString().match(/https:\/\/.*\.trycloudflare\.com/)
if (output) {
console.log(`Tunnel up at: ${output[0]}`)
}
});
cloudflared.on('close', (code) => {
console.log(`cloudflared exited with code ${code}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment