I can't for the life of me get a svelte project to call a function.

I've tried putting the function code in routes/hello.js and routes/hello.server.js According to the docs: https://kit.svelte.dev/docs/adapter-cloudflare#notes Functions contained in the /functions directory at the project's root will not be included in the deployment, which is compiled to a single _worker.js file. Functions should be implemented as server endpoints in your SvelteKit app. My "function" code is: routes/hello.js: // Handle GET requests export const GET = () => { return new Response('hello ' + String(Math.random())); }; and in routes/+page.svelte: async function handleHelloSubmit(event) { event.preventDefault(); try { const response = await fetch('/hello'); if (!response.ok) { throw new Error('Failed to fetch hello message'); } helloMessage = await response.text(); } catch (error) { console.error('Error fetching hello message:', error); helloMessage = 'An error occurred while fetching the hello message.'; } } I don't get it. npm run build works and wrangler pages deploy too. I've deployed it to https://selfie.pages.dev/ so https://selfie.pages.dev/hello returns 404. Any ideas would be greatly apprecaited. I'm just trying to get this scaffolding working so I can write the real functions... But I'm stuck on just calling functions from Svelte.
SvelteKit docs
Cloudflare Pages • SvelteKit documentation
5 Replies
ab90..
ab90..OP4mo ago
Here's all my code... https://github.com/adamb/selfie
GitHub
GitHub - adamb/selfie
Contribute to adamb/selfie development by creating an account on GitHub.
Hard@Work
Hard@Work4mo ago
When using SvelteKit(or another framework like it), you need to use their version of API Routes(+server.js/ts) You can't use Functions directly Oh wait Sorry, Misread, let me restart Try renaming hello.js to hello/+server.js
ab90..
ab90..OP4mo ago
wow! that works. Nice if it was documented somewhere.
Hard@Work
Hard@Work4mo ago
Functions should be implemented as server endpoints in your SvelteKit app.
SvelteKit docs
Routing • SvelteKit documentation
ab90..
ab90..OP4mo ago
thanks! I apprecaite it. I looked at that page but didn't grok that far down.
Want results from more Discord servers?
Add your server