Node.js - api.route()

Creates a new route (path) within an API.

import { api } from '@nitric/sdk'
const customersRoute = api('public').route('/customers')

Parameters

  • Name
    match
    Required
    Required
    Type
    string
    Description

    The path matcher to use for this route. Calling route on the same API more than once with the same matcher will return the same route object. 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
    options
    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.

Notes

The middleware property on the options param is useful for applying universal middleware such as CORS headers or Auth, across an entire route. However, if methods aren't registered, the route won't be deployed. If you need to run the same handler for all methods on a route, you should use route.all()

Examples

Create a route

import { api } from '@nitric/sdk'
const customersRoute = api('public').route('/customers')

Create a route with path params

Route paths can include dynamic parameters. These values will automatically be parsed and provided in the context object for your middleware and handlers as a string.

For example, if you have a customers path and you want to include a customerId param you would define the route like this.

import { api } from '@nitric/sdk'
const customerRoute = api('public').route('/customers/:customerId')

Create a route with middleware

import { api } from '@nitric/sdk'
import { authMiddleware } from '../middleware'
const privateApi = api('private').route('/customers', {
middleware: authMiddleware,
})
Last updated on Oct 16, 2024