I noticed some weird behaviour; CloudFlare Workers returns 404, even on routes that are found.
Steps to reproduce:
- Run
wrangler generate my-worker-with-router https://github.com/cloudflare/worker-template-router
(as per the official documentation) - Fill in your account_id
- Run
wrangler publish
orwrangler dev
- 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
- 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: GitHub - simplenotezy/cloudflare-workers-returns-404