addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
function handleRequest(request) {
const url = new URL(request.url)
const {
href,
protocol,
hostname,
port,
pathname,
search,
hash
} = url
const htmlContent = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Request URL Details</title>
</head>
<body>
<h1>Request URL Details</h1>
<p><strong>Complete URL:</strong> <a href="${href}">${href}</a></p>
<p><strong>Protocol:</strong> ${protocol}</p>
<p><strong>Hostname:</strong> ${hostname}</p>
${port ? `<p><strong>Port:</strong> ${port}</p>` : ''}
<p><strong>Pathname:</strong> ${pathname}</p>
<p><strong>Search:</strong> ${search}</p>
${hash ? `<p><strong>Hash:</strong> ${hash}</p>` : ''}
</body>
</html>
`;
return new Response(htmlContent, {
headers: {
'Content-Type': 'text/html;charset=UTF-8'
}
})
}
Was this page helpful?