Skip to content

Instantly share code, notes, and snippets.

@NazCodeland
Last active June 23, 2024 23:54
Show Gist options
  • Save NazCodeland/2c53255d9088d27482714edfbe847023 to your computer and use it in GitHub Desktop.
Save NazCodeland/2c53255d9088d27482714edfbe847023 to your computer and use it in GitHub Desktop.
Handling multiple GET requests within one endpoint in SvelteKit
// CREDIT: https://github.com/karimfromjordan
// https://kit.svelte.dev/docs/routing#server-fallback-method-handler
export async function fallback({ url, request }) {
if (request.method === 'GET' && url.pathame === '/api/x') {
// ...
}
if (request.method === 'GET' && url.pathame === '/api/y') {
// ...
}
if (request.method === 'GET' && url.pathame === '/api/z') {
// ...
}
// else return error
}
// or
export async function GET({ url }) {
if (url.pathname === '/api/x') {
// Handle GET request for /api/x
}
if (url.pathname === '/api/y') {
// Handle GET request for /api/y
}
if (url.pathname === '/api/z') {
// Handle GET request for /api/z
}
// else return error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment