I am creating a REST api using sveltekit and have a server.ts file for accessing data by id.
The route is defined as: <routes>/item/[itemId]/ server.ts
How should I access the itemId from a server route?
What I have tried
If this were a page.svelte file I would access it as follows:
import { page } from '$app/stores';
const { itemId } = $page.params;
However my understanding is that $app/stores not a server side concept (and indeed I am warned that $page does not exist).
CodePudding user response:
I think what you are trying to do is this.
// server.ts
import { json, type RequestHandler } from "@sveltejs/kit";
export const GET: RequestHandler = ({ params }) => {
console.log(params) // { itemId: '173' }
return json(params)
}
You can access this endpoint from your client-side using fetch or if this data needs to be page data/params then you can convert it to a page.server.ts or page.ts or directly access them via $page.params
