I am trying to use a svelte app to call an api route. I made a simple svelte app with npm init svelte@next svelte-api-calling
. I added the file routes/api/callthis/[name].ts
, and it looks like this:
export async function get({ params }) {
const {name} = params;
console.log(name);
};
I start the app with npm run dev
. I tried to call this api route by typing http://localhost:5173/api/callthis/check
into my browser. I expected my console to log check
. It gave me a 404 error. How do I get this route to work?
This is not how routing works in SvelteKit.
First if you want an api route you make a file +server.js
(or ts), so in your case the full structure would be routes/api/callthis/[name]/+server.js
(the code in the file itself can be similar)
Second this api runs on the server (it is after all an api), so the console.log would be in the server console and not in the browser.
You are also not returning anything from the api (yet)
This answer holds for the version of SvelteKit as it is on 20.02.2023, when using StackOverflow always remember to check the date because technology can change rapidly