@fastify/routes
TypeScript icon, indicating that this package has built-in type declarations

5.1.0 • Public • Published

@fastify/routes

CI NPM version js-standard-style

This plugin decorates a Fastify instance with routes, which is a Map of registered routes. Note that you have to register this plugin before registering any routes so that it can collect all of 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')()

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

Readme

Keywords

Package Sidebar

Install

npm i @fastify/routes

Weekly Downloads

5,655

Version

5.1.0

License

MIT

Unpacked Size

12.3 kB

Total Files

12

Last publish

Collaborators

  • gurgunday
  • metcoder95
  • galvez
  • simenb
  • coopflow
  • simoneb
  • rafaelgss
  • starptech
  • delvedor
  • matteo.collina
  • allevo
  • jsumners
  • zekth
  • eomm
  • fox1t
  • airhorns
  • kibertoad
  • climba03003
  • is2ei
  • fdawgs