Skip to content

Instantly share code, notes, and snippets.

@aquapi
Last active August 10, 2023 19:17
Show Gist options
  • Save aquapi/40cd6efcc124cb3f9d625cc151202909 to your computer and use it in GitHub Desktop.
Save aquapi/40cd6efcc124cb3f9d625cc151202909 to your computer and use it in GitHub Desktop.
Stric example code
import { Router, macro } from '@stricjs/router';
import { qs } from '@stricjs/utils';
// Create a query parser that only search for key `name` in the query
const parse = qs.searchKey('name');
export default new Router({ base: 'http://localhost:3000' })
// Return 'Hi' on every requests to '/'
.get('/', macro('Hi'))
// Yield back the JSON that the request sent
.post('/json', ctx => ctx.json().then(Response.json))
// Response with the `id` URL parameter value and the `name` query parameter value
.get('/id/:id', ctx => new Response(ctx.params.id + ' ' + parse(ctx)))
// Handle 404
.use(404);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment