|
|
|
|
|
|
|
|
|
|
|
export default { |
|
|
async fetch(request, env, ctx) { |
|
|
const url = new URL(request.url); |
|
|
|
|
|
|
|
|
if (url.pathname === '/') { |
|
|
return new Response('Hello from Cloudflare Worker! ๐', { |
|
|
headers: { |
|
|
'Content-Type': 'text/plain; charset=utf-8' |
|
|
} |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
if (url.pathname === '/api/time') { |
|
|
return new Response(JSON.stringify({ |
|
|
timestamp: new Date().toISOString(), |
|
|
timezone: 'UTC' |
|
|
}), { |
|
|
headers: { |
|
|
'Content-Type': 'application/json' |
|
|
} |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
if (url.pathname === '/api/headers') { |
|
|
const headers = {}; |
|
|
request.headers.forEach((value, key) => { |
|
|
headers[key] = value; |
|
|
}); |
|
|
|
|
|
return new Response(JSON.stringify({ |
|
|
method: request.method, |
|
|
headers: headers, |
|
|
cf: request.cf |
|
|
}, null, 2), { |
|
|
headers: { |
|
|
'Content-Type': 'application/json' |
|
|
} |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
if (url.pathname === '/api/echo' && request.method === 'POST') { |
|
|
const body = await request.text(); |
|
|
|
|
|
return new Response(JSON.stringify({ |
|
|
message: 'Echo response', |
|
|
received: body, |
|
|
length: body.length |
|
|
}), { |
|
|
headers: { |
|
|
'Content-Type': 'application/json' |
|
|
} |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
return new Response('404 Not Found', { |
|
|
status: 404, |
|
|
headers: { |
|
|
'Content-Type': 'text/plain' |
|
|
} |
|
|
}); |
|
|
} |
|
|
} |
|
|
|