Home / Function/ fourOhFour() — fastify Function Reference

fourOhFour() — fastify Function Reference

Architecture documentation for the fourOhFour() function in four-oh-four.js from the fastify codebase.

Entity Profile

Dependency Diagram

graph TD
  656a7e23_d072_7cc5_d552_ab34d1706056["fourOhFour()"]
  79fed956_39c1_bd52_8cb2_0785a23f3290["four-oh-four.js"]
  656a7e23_d072_7cc5_d552_ab34d1706056 -->|defined in| 79fed956_39c1_bd52_8cb2_0785a23f3290
  style 656a7e23_d072_7cc5_d552_ab34d1706056 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

lib/four-oh-four.js lines 31–188

function fourOhFour (options) {
  const { logger, disableRequestLogging } = options

  // 404 router, used for handling encapsulated 404 handlers
  const router = FindMyWay({ onBadUrl: createOnBadUrl(), defaultRoute: fourOhFourFallBack })
  let _onBadUrlHandler = null

  return { router, setNotFoundHandler, setContext, arrange404 }

  function arrange404 (instance) {
    // Change the pointer of the fastify instance to itself, so register + prefix can add new 404 handler
    instance[kFourOhFourLevelInstance] = instance
    instance[kCanSetNotFoundHandler] = true
    // we need to bind instance for the context
    router.onBadUrl = router.onBadUrl.bind(instance)
    router.defaultRoute = router.defaultRoute.bind(instance)
  }

  function basic404 (request, reply) {
    const { url, method } = request.raw
    const message = `Route ${method}:${url} not found`
    const resolvedDisableRequestLogging = typeof disableRequestLogging === 'function' ? disableRequestLogging(request.raw) : disableRequestLogging
    if (!resolvedDisableRequestLogging) {
      request.log.info(message)
    }
    reply.code(404).send({
      message,
      error: 'Not Found',
      statusCode: 404
    })
  }

  function createOnBadUrl () {
    return function onBadUrl (path, req, res) {
      const fourOhFourContext = this[kFourOhFourLevelInstance][kFourOhFourContext]
      const id = getGenReqId(fourOhFourContext.server, req)
      const childLogger = createChildLogger(fourOhFourContext, logger, req, id)
      const request = new Request(id, null, req, null, childLogger, fourOhFourContext)
      const reply = new Reply(res, request, childLogger)

      _onBadUrlHandler(request, reply)
    }
  }

  function setContext (instance, context) {
    const _404Context = Object.assign({}, instance[kFourOhFourContext])
    _404Context.onSend = context.onSend
    context[kFourOhFourContext] = _404Context
  }

  function setNotFoundHandler (opts, handler, avvio, routeHandler) {
    // First initialization of the fastify root instance
    if (this[kCanSetNotFoundHandler] === undefined) {
      this[kCanSetNotFoundHandler] = true
    }
    if (this[kFourOhFourContext] === undefined) {
      this[kFourOhFourContext] = null
    }

    const _fastify = this
    const prefix = this[kRoutePrefix] || '/'

    if (this[kCanSetNotFoundHandler] === false) {
      throw new Error(`Not found handler already set for Fastify instance with prefix: '${prefix}'`)
    }

    if (typeof opts === 'object') {
      if (opts.preHandler) {
        if (Array.isArray(opts.preHandler)) {
          opts.preHandler = opts.preHandler.map(hook => hook.bind(_fastify))
        } else {
          opts.preHandler = opts.preHandler.bind(_fastify)
        }
      }

      if (opts.preValidation) {
        if (Array.isArray(opts.preValidation)) {
          opts.preValidation = opts.preValidation.map(hook => hook.bind(_fastify))
        } else {
          opts.preValidation = opts.preValidation.bind(_fastify)
        }

Domain

Subdomains

Defined In

Frequently Asked Questions

What does fourOhFour() do?
fourOhFour() is a function in the fastify codebase, defined in lib/four-oh-four.js.
Where is fourOhFour() defined?
fourOhFour() is defined in lib/four-oh-four.js at line 31.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free