Docs / Configuration / routes.ts

3.4. routes.ts

Define how web requests are routed to controllers. The Route objects are modeled on the hapi.js Route Specification. Some plugins such as spool-tapestries may auto-generate Route configuration based on other facets of the application.

export const routes = {

 /**
  * GET requests to /default/info will invoke DefaultController.info
  */
  '/default/info': {
    'GET': {
      handler: 'DefaultController.info'
    }
  }
}

path (Object Key)

The route Object Key (or path) will handle requests to URLs that match.

handler

A string that defines the Fabrix Controller method that will handle the request.

method

The Route method can be the string of a single http method (e.g. POST) or an array of the http methods that will be accepted for this route.

Path Parameters

Path parameters can be defined, and will be passed into their handlers. Full Specification.

// config/routes.ts
export const routes = {
  '/map/tile/{x}/{y}/{z?}': {
    'GET': {
      handler: 'MapController.getTile'
    }
  }
}
// api/controllers/MapController.ts
export class MapController extends Controller {

  /**
   * @param request.params.x  The x coordinate of the map tile
   * @param.request.params.y  The y coordinate of the map tile
   * @param.request.params.z  The zoom level of the map (optional)
   */
  getTile (request, reply) {
    const { x, y, z } = request.params

    reply(MapService.getTile({ x, y, z }))
  }
}

config

Each route can define other custom properties in a nested config object. Full Specification. Below are some common uses.

Self-Documenting Routes

Some doc generation tools, such as Swagger, can use the config object to glean contextual information about the route. Full

export const routes = {
  // ...
  '/map/tile/{x}/{y}/{z?}': {
    'GET': {
      handler: 'MapController.getTile',
      config: {
        description: 'Render a map Tile',
        notes: 'The "z" param will default to config.map.defaultZoom if not given',
        tags: [ 'api', 'map', 'tile' ]
      }
    }
  }
  // ...
}

Access Control

Pre-requisites can be defined for a route, which are a list of Fabrix Policies that must pass before the route handler is invoked.

export const routes = {
    // ...
    '/map/tile/{x}/{y}/{z?}': {
      'GET': {
        handler: 'MapController.getTile',
        config: {
        /**
         * Ensure that the client has a valid API key for this Map tile request
         */
          pre: [
            'MapPolicy.verifyApiKey'
          ],
    
         /**
          * Only allow requests from *.fabrix.app domains
          */
          cors: {
            origin: [
              '*.fabrix.app'
            ]
          }
        }
      } 
    }
    // ...
}