Skip to content

Commit 6ad9d3a

Browse files
committed
Override request URL
1 parent 05d725f commit 6ad9d3a

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ async function reportMetric(request, response, startTime, endTime, env) {
5050

5151
export default {
5252
async fetch(request, env, ctx) {
53+
const url = new URL(request.url);
54+
const newUrl = `${env.REQUEST_URL}${url.pathname}${url.search}`;
55+
// override the request with the new URL
56+
request = new Request(newUrl, request);
5357
const reqStartTime = Date.now();
54-
const response = await fetch('https://example.com');
58+
const response = await fetch(request);
5559
const reqEndTime = Date.now();
60+
5661
ctx.waitUntil(reportMetric(request, response, reqStartTime, reqEndTime, env));
5762
return response;
5863
}

wrangler.toml.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ compatibility_date = "2024-12-05"
44
compatibility_flags = ["nodejs_compat"]
55

66
[vars]
7+
REQUEST_URL = "https://example.com"
78
INFLUX_METRIC = "example"
89
INFLUX_URL = "http://localhost:8086"
910
INFLUX_TOKEN = "example"

0 commit comments

Comments
 (0)