Not showing the expected output on certain urls

I am trying to show response according to the requested URL but i am getting only the homepage on each record whether i enter /onduty or /onduty-how how can i show the response according to the request url

here is the code

const growthfilePage = `

Hello World

This markup was generated by a Cloudflare Worker.

` const onduty = `

OnDuty

This markup was generated by a Cloudflare Worker.

const homepage =

landing

This markup was generated by a Cloudflare Worker.

` async function handleRequest(request) { console.log(request.url) if ( request.url === 'https://staticpage.practice.workers.dev/onduty' || request.url === 'https://staticpage.practice.workers.dev/ondDuty' ) { return new Response(onduty, { headers: { 'content-type': 'text/html;charset=UTF-8', }, }) } else if ( request.url === 'https://staticpage.practice.workers.dev/onduty-how' ) { return new Response(homepage, { headers: { 'content-type': 'text/html;charset=UTF-8', }, }) } return new Response(growthfilePage, { headers: { 'content-type': 'text/html;charset=UTF-8', }, }) }

addEventListener(‘fetch’, (event) => {
return event.respondWith(handleRequest(event.request))
})

according to the above code this works fine but when i am using the below code i am getting the problem
const growthfilePage = `

Hello World

This markup was generated by a Cloudflare Worker.

` const onduty = `

OnDuty

This markup was generated by a Cloudflare Worker.

const homepage =

landing

This markup was generated by a Cloudflare Worker.

` async function handleRequest(request) { console.log(request.url) if ( request.url === 'https://bewithrits.tech/onduty' || request.url === 'https://bewithrits.tech/onDuty' ) { return new Response(onduty, { headers: { 'content-type': 'text/html;charset=UTF-8', }, }) } else if (request.url === 'https://bewithrits.tech/onduty-how') { return new Response(homepage, { headers: { 'content-type': 'text/html;charset=UTF-8', }, }) } return new Response(growthfilePage, { headers: { 'content-type': 'text/html;charset=UTF-8', }, }) }

addEventListener(‘fetch’, (event) => {
return event.respondWith(handleRequest(event.request))
})

Hi @singhamarjeet0045 ,

While I’m not exactly sure what went wrong with your example, I would like to point out that URL API mentioned in https://developers.cloudflare.com/workers/runtime-apis/web-standards might be helpful for your case. With URL API, it is much easier to extract the pathname for further processing.
Reference: https://developer.mozilla.org/en-US/docs/Web/API/URL

Here’s a simpler example which you can try on your Cloudflare Workers:

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

/**
 * Respond to the request
 * @param {Request} request
 */
async function handleRequest(request) {
  const requestURL = new URL(request.url)
  const content = {
    "default": "<html><body><h1>Hello, world!</h1></body></html>",
    "/foo": "<html><body><h1>Foo</h1></body></html>",
    "/bar": "<html><body><h1>Bar</h1></body></html>",
    "/baz": "<html><body><h1>Baz</h1></body></html>"
  }
  body = requestURL.pathname in content 
         ? content[requestURL.pathname] 
         : content.default
  return new Response(body, {'headers':{'content-type': 'text/html'}})
}

At the time of writing, the above example is live in the community-203606.out.workers.dev subdomain.
Here are some links you can try:

I hope the above helps you with Cloudflare Workers.
Have a great day!

1 Like

This topic was automatically closed after 31 days. New replies are no longer allowed.