Unable to test Cloudflare Page Function: [wrangler:err] Error: No response!

I have a simple static site to which I want to add a Page Function. So far, I've read the tutorials at these links to get a sense for how this works (I'm migrating from Netlify): https://developers.cloudflare.com/pages/functions/get-started/ https://developers.cloudflare.com/pages/how-to/refactor-a-worker-to-pages-functions/ https://developers.cloudflare.com/pages/functions/typescript/ https://developers.cloudflare.com/pages/tutorials/build-an-api-with-pages-functions/ Here is what I did: 1. Installed wrangler (v3.72.2). 2. Created a functions/api/comments.ts file in my Pages project directory.
export function onRequestGet() {
return Response.json('test', { status: 200 });
}
export function onRequestGet() {
return Response.json('test', { status: 200 });
}
3. Created the following wrangler.toml file at the root of my Pages directory:
# Top-level configuration
name = "comments"
main = "./functions/api/comments.ts"
send_metrics = false
compatibility_date = "2022-07-12"
node_compat = true
workers_dev = false
# Top-level configuration
name = "comments"
main = "./functions/api/comments.ts"
send_metrics = false
compatibility_date = "2022-07-12"
node_compat = true
workers_dev = false
4. Ran wrangler dev to start up the local server. But when I navigate to localhost:8787 (either in my browser or via curl), I get [wrangler:err] Error: No response! The only way I'm able to get my function to work is if I convert it to a proper worker with a default export containing a fetch function as shown here: Are Page Functions deprecated? If not, does anyone know what I'm doing wrong?
No description
5 Replies
aleksandr
aleksandrOP3mo ago
Full stack trace (sorry, too long to post as plaintext)
No description
aleksandr
aleksandrOP3mo ago
I also get this warning in my server console when following the quickstart example
▲ [WARNING] The entrypoint functions/api/comments.ts has exports like an ES Module, but hasn't defined a default export like a module worker normally would. Building the worker using "service-worker" format...
▲ [WARNING] The entrypoint functions/api/comments.ts has exports like an ES Module, but hasn't defined a default export like a module worker normally would. Building the worker using "service-worker" format...
James
James3mo ago
You should be using wrangler pages dev if you want to use Functions You’re trying to load a single function as an entire worker right now, and that doesn’t work Pages automatically bundles your functions into a worker. Simply run wrangler pages dev public with your functions next to your public dir, and where public is a folder of static assets like HTML
aleksandr
aleksandrOP3mo ago
Ah, thanks James! Let me give that a shot. Okay, that resolved my issue (I'm running into other unrelated errors now, but this has unblocked me). Thanks so much!
James
James3mo ago
Happy to help!
Want results from more Discord servers?
Add your server