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


match required string

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


...middleware required HttpMiddleware

One or more middleware functions to use as the handler for HTTP requests. Handlers can be sync or async.


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 functions as a single method handler

When multiple functions 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);