Node.js - api.get()

Register an API route and set a specific HTTP GET handler on that route.

This method is a convenient short version of api().route().get()

import { api } from '@nitric/sdk'
api('public').get('/customers', (ctx) => {
// construct response for the GET: /customers request...
const responseBody = {}
ctx.res.json(responseBody)
})

Parameters

  • Name
    match
    Type
    string
    Description

    The path matcher to use for the route. Matchers accept path parameters in the form of a colon prefixed string. The string provided will be used as that path parameter's name when calling middleware and handlers. See create a route with path params

  • Name
    middleware
    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
    Type
    object
    Description

    Additional options when creating method.

    • Name
      security
      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'
api('public').get('/customers', (ctx) => {
// construct response for the GET: /customers 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 getAllCustomers = (ctx) => {
// construct response for the GET: /customers request...
const responseBody = {}
ctx.res.json(responseBody)
}
api('public').get('/customers', [validate, getAllCustomers])
Last updated on Oct 11, 2024