Last active
July 27, 2023 17:16
-
-
Save NeuronButter/821a7cb890c2d275ab39becea028a961 to your computer and use it in GitHub Desktop.
Programmatically use cloudflared Quick Tunnels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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