Skip to content

Commit d37162c

Browse files
committed
docs: add example code of service api
1 parent 39dc3bd commit d37162c

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,36 @@ Connections Ready! [
123123
]
124124
tunnel process exited with code 0
125125
```
126+
127+
### Service
128+
129+
Checkout [`examples/service.js`](examples/service.js).
130+
131+
```js
132+
import { service } from "cloudflared";
133+
134+
console.log("Cloudflared Service Example.");
135+
main();
136+
137+
async function main() {
138+
if (service.exists()) {
139+
console.log("Service is running.");
140+
const current = service.current();
141+
for (const { service, hostname } of current.config.ingress) {
142+
console.log(` - ${service} -> ${hostname}`);
143+
}
144+
console.log("metrics server:", current.metrics);
145+
} else {
146+
console.log("Service is not running.");
147+
}
148+
}
149+
```
150+
151+
```sh
152+
❯ node examples/service.js
153+
Cloudflared Service Example.
154+
Service is running.
155+
- http://localhost:12345 -> sub.example.com
156+
- http_status:404 -> undefined
157+
metrics server: 127.0.0.1:49177/metrics
158+
```

examples/service.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { service } from "../lib/lib.js";
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+
}

0 commit comments

Comments
 (0)