Node.js - api.delete()

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

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

import { api } from '@nitric/sdk'
const PARAM_ID = 'customerId'
api('public').delete(`/customers/:${PARAM_ID}`, (ctx) => {
// construct response for the DELETE: /customers request...
const responseBody = {}
ctx.res.json(responseBody)
})

Parameters

  • Name
    match
    Required
    Required
    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
    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 DELETE requests

import { api } from '@nitric/sdk'
const PARAM_ID = 'customerId'
api('public').delete(`/customers/:${PARAM_ID}`, (ctx) => {
const id = ctx.req.params[PARAM_ID]
// handle the DELETE 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 PARAM_ID = 'customerId'
const deleteCustomer = (ctx) => {
const id = ctx.req.params[PARAM_ID]
// handle the DELETE request...
const responseBody = {}
ctx.res.json(responseBody)
}
api('public').delete(`/customers/:${PARAM_ID}`, [validate, deleteCustomer])

Access the request body

The DELETE request body is accessible from the ctx.req object.

import { api } from '@nitric/sdk'
api('public').delete(`/customers`, (ctx) => {
const customerData = ctx.req.data
// parse, validate and store the request payload...
})
Last updated on Nov 8, 2024