Home / Function/ createServer() — fastify Function Reference

createServer() — fastify Function Reference

Architecture documentation for the createServer() function in server.js from the fastify codebase.

Entity Profile

Dependency Diagram

graph TD
  55bc576c_6c04_1347_602f_f32a2197069f["createServer()"]
  39d7da74_a631_4522_4e09_0cc36c64a8f8["server.js"]
  55bc576c_6c04_1347_602f_f32a2197069f -->|defined in| 39d7da74_a631_4522_4e09_0cc36c64a8f8
  f8a56543_30d0_fd17_5f30_f1ddc20ab439["getServerInstance()"]
  f8a56543_30d0_fd17_5f30_f1ddc20ab439 -->|calls| 55bc576c_6c04_1347_602f_f32a2197069f
  f8a56543_30d0_fd17_5f30_f1ddc20ab439["getServerInstance()"]
  55bc576c_6c04_1347_602f_f32a2197069f -->|calls| f8a56543_30d0_fd17_5f30_f1ddc20ab439
  style 55bc576c_6c04_1347_602f_f32a2197069f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

lib/server.js lines 27–149

function createServer (options, httpHandler) {
  const server = getServerInstance(options, httpHandler)

  // `this` is the Fastify object
  function listen (
    listenOptions = { port: 0, host: 'localhost' },
    cb = undefined
  ) {
    if (typeof cb === 'function') {
      if (cb.constructor.name === 'AsyncFunction') {
        FSTWRN003('listen method')
      }

      listenOptions.cb = cb
    }
    if (listenOptions.signal) {
      if (typeof listenOptions.signal.on !== 'function' && typeof listenOptions.signal.addEventListener !== 'function') {
        throw new FST_ERR_LISTEN_OPTIONS_INVALID('Invalid options.signal')
      }

      // copy the current signal state
      this[kState].aborted = listenOptions.signal.aborted

      if (this[kState].aborted) {
        return this.close()
      } else {
        const onAborted = () => {
          this[kState].aborted = true
          this.close()
        }
        listenOptions.signal.addEventListener('abort', onAborted, { once: true })
      }
    }

    // If we have a path specified, don't default host to 'localhost' so we don't end up listening
    // on both path and host
    // See https://github.com/fastify/fastify/issues/4007
    let host
    if (listenOptions.path == null) {
      host = listenOptions.host ?? 'localhost'
    } else {
      host = listenOptions.host
    }
    if (!Object.hasOwn(listenOptions, 'host') ||
      listenOptions.host == null) {
      listenOptions.host = host
    }
    if (host === 'localhost') {
      listenOptions.cb = (err, address) => {
        if (err) {
          // the server did not start
          cb(err, address)
          return
        }

        multipleBindings.call(this, server, httpHandler, options, listenOptions, () => {
          this[kState].listening = true
          cb(null, address)
          onListenHookRunner(this)
        })
      }
    } else {
      listenOptions.cb = (err, address) => {
        // the server did not start
        if (err) {
          cb(err, address)
          return
        }
        this[kState].listening = true
        cb(null, address)
        onListenHookRunner(this)
      }
    }

    // https://github.com/nodejs/node/issues/9390
    // If listening to 'localhost', listen to both 127.0.0.1 or ::1 if they are available.
    // If listening to 127.0.0.1, only listen to 127.0.0.1.
    // If listening to ::1, only listen to ::1.

    if (cb === undefined) {
      const listening = listenPromise.call(this, server, listenOptions)

Domain

Subdomains

Defined In

Frequently Asked Questions

What does createServer() do?
createServer() is a function in the fastify codebase, defined in lib/server.js.
Where is createServer() defined?
createServer() is defined in lib/server.js at line 27.
What does createServer() call?
createServer() calls 1 function(s): getServerInstance.
What calls createServer()?
createServer() is called by 1 function(s): getServerInstance.

Analyze Your Own Codebase

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

Try Supermodel Free