Skip to content

Commit 9971329

Browse files
committed
docs: add cjs example
1 parent 41af27e commit 9971329

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

examples/service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { service } from "cloudflared";
1+
const { service } = require("cloudflared");
22

33
console.log("Cloudflared Service Example.");
44
main();

examples/service.mjs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { service } from "cloudflared";
2+
3+
console.log("Cloudflared Service Example.");
4+
main();
5+
6+
async function main() {
7+
if (service.exists()) {
8+
console.log("Service is running.");
9+
const current = service.current();
10+
for (const { service, hostname } of current.config.ingress) {
11+
console.log(` - ${service} -> ${hostname}`);
12+
}
13+
console.log("metrics server:", current.metrics);
14+
} else {
15+
console.log("Service is not running.");
16+
}
17+
}

examples/tunnel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { tunnel } from "cloudflared";
1+
const { tunnel } = require("cloudflared");
22

33
console.log("Cloudflared Tunnel Example.");
44
main();

examples/tunnel.mjs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { tunnel } from "cloudflared";
2+
3+
console.log("Cloudflared Tunnel Example.");
4+
main();
5+
6+
async function main() {
7+
// run: cloudflared tunnel --hello-world
8+
const { url, connections, child, stop } = tunnel({ "--hello-world": null });
9+
10+
// show the url
11+
console.log("LINK:", await url);
12+
13+
// wait for the all 4 connections to be established
14+
const conns = await Promise.all(connections);
15+
16+
// show the connections
17+
console.log("Connections Ready!", conns);
18+
19+
// stop the tunnel after 15 seconds
20+
setTimeout(stop, 15_000);
21+
22+
child.on("exit", (code) => {
23+
console.log("tunnel process exited with code", code);
24+
});
25+
}

0 commit comments

Comments
 (0)