Skip to content

Expose fastify options to plugins #1444

@coreyfarrell

Description

@coreyfarrell

🚀 Feature Proposal

Expose the options used to create the fastify server in a way that is accessible to plugins.

Motivation

In some cases it might be necessary to register routes differently, for example if ignoreTrailingSlash is enabled it is impossible to register separate routes for /dir vs /dir/.

Example

fastify/fastify-static#93 adds an option to send a 301 redirect when a request for a directory without the trailing slash is received. This option currently cannot be combined with wildcard: false if ignoreTrailingSlash is enabled on the server.

const fastify = require('fastify')({
  ignoreTrailingSlash: true
})

fastify.register(async (instance, opts) => {
  if (opts.initialConf.ignoreTrailingSlash) {
    // This registers `/dir` and `/dir/` so a shared handler needs to
    // determine which was requested and call handleDir or handleDirSlash.
    instance.get('/dir', handleDirOrDirSlash)
  } else {
    instance.get('/dir', handleDir)
    instance.get('/dir/', handleDirSlash)
  }
})

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions