Node.js - api.route.get()
Register a handler for HTTP GET requests to the route.
import { api } from '@nitric/sdk'const customerRoute = api('public').route(`/customers`)customerRoute.get((ctx) => {// construct response for the GET: /customers request...const responseBody = {}ctx.res.json(responseBody)})
Parameters
- Name
middleware
- Required
- Required
- Type
- HttpMiddleware | HttpMiddleware[]
- Description
One or more middleware services to use as the handler for HTTP requests. Handlers can be sync or async
- Name
opts
- Optional
- Optional
- Type
- object
- Description
Additional options when creating method.
- Name
security
- Optional
- Optional
- Type
- OidcOptions[]
- Description
Security rules to apply with scopes to the entire API.
Examples
Register a handler for GET requests
import { api } from '@nitric/sdk'const customerRoute = api('public').route(`/customers`)customerRoute.get((ctx) => {// handle the GET request...const responseBody = {}ctx.res.json(responseBody)})
Chain services as a single method handler
When multiple services are provided they will be called as a chain. If one succeeds, it will move on to the next. This allows middleware to be composed into more complex handlers.
import { api } from '@nitric/sdk'import { validate } from '../middleware'const getCustomer = (ctx) => {// handle the GET request...const responseBody = {}ctx.res.json(responseBody)}const customerRoute = api('public').route(`/customers`)customerRoute.get([validate, getCustomer])
Last updated on Dec 19, 2024