Skip to content

Files

Latest commit

eb63a85 · Jan 31, 2025

History

History
Cannot retrieve ahead/behind information for this branch.

Folders and files

NameName
Last commit message
Last commit date
Jan 31, 2025
Jan 7, 2025
Dec 8, 2024
Sep 28, 2018
Oct 7, 2023
Feb 8, 2024
Dec 5, 2022
Sep 28, 2018
Jan 31, 2025
Dec 8, 2024
Jan 7, 2025
Jan 11, 2025

README.md

@fastify/routes

CI NPM version neostandard javascript style

This plugin decorates a Fastify instance with routes, which is a Map of registered routes. Note that you have to await the registration of this plugin before registering any routes so that @fastify/routes can collect them.

Data Structure

The fastify.routes Map has a key for each path any route has been registered, which points to an array of routes registered on that path. There can be more than one route for a given path if there are multiple routes added with different methods or different constraints.

  {
    '/hello': [
      {
        method: 'GET',
        url: '/hello',
        schema: { ... },
        handler: Function,
        prefix: String,
        logLevel: String,
        bodyLimit: Number,
        constraints: undefined,
      },
      {
        method: 'POST',
        url: '/hello',
        schema: { ... },
        handler: Function,
        prefix: String,
        logLevel: String,
        bodyLimit: Number,
        constraints: { ... },
      }
    ]
  }

Example

const fastify = require("fastify")();

(async () => {
  await fastify.register(require("@fastify/routes"));
  fastify.get("/hello", {}, (request, reply) => {
    reply.send({ hello: "world" });
  });

  fastify.listen({ port: 3000 }, (err, address) => {
    if (err) {
      console.error(err);
      return;
    }
    console.log(fastify.routes);
    /* will output a Map with entries:
    {
      '/hello': [
        {
          method: 'GET',
          url: '/hello',
          schema: Object,
          handler: <Function>,
          prefix: <String>,
          logLevel: <String>,
          bodyLimit: <Number>
        }
      ]
    }
    */
  });
})();

License

MIT License