cloudflare / worker-template-router

231 stars 84 forks source link

Always returning 404 #33

Closed simplenotezy closed 3 years ago

simplenotezy commented 3 years ago

I noticed some weird behaviour; CloudFlare Workers returns 404, even on routes that are found.

Steps to reproduce:

1) Run wrangler generate my-worker-with-router https://github.com/cloudflare/worker-template-router (as per the official documentation) 2) Fill in your account_id 3) Run wrangler publish or wrangler dev 4) Send a CURL request to any given route that exists in the starter template, e.g. curl -I http://127.0.0.1:8787/foo 5) Notice that the responseCode is 404, even though it is found.

I have also tried to manually override the response code, to e.g. 200, but it still returns 404.

function handler(request) {
    const init = {
        headers: { 'content-type': 'application/json' },
        status: 200 // still returns a 404
    }
    const body = JSON.stringify({ some: 'json' })
    return new Response(body, init)
}

Here's a repo if you'd like to see the exact code that fails: https://github.com/simplenotezy/cloudflare-workers-returns-404

simplenotezy commented 3 years ago

My mistake. I was doing curl -I which then sends a HEAD request. I didn't set any r.head, only r.get which was why I got a 404. So the strange mystery is resolved.