Last active
August 10, 2023 19:17
-
-
Save aquapi/40cd6efcc124cb3f9d625cc151202909 to your computer and use it in GitHub Desktop.
Stric example code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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