Home / Function/ setup() — fastify Function Reference

setup() — fastify Function Reference

Architecture documentation for the setup() function in custom-http-server.test.js from the fastify codebase.

Entity Profile

Dependency Diagram

graph TD
  252fb244_8943_eb79_6be9_a5aa544851e0["setup()"]
  47c12813_f614_8aeb_5374_fcde769a3d55["custom-http-server.test.js"]
  252fb244_8943_eb79_6be9_a5aa544851e0 -->|defined in| 47c12813_f614_8aeb_5374_fcde769a3d55
  style 252fb244_8943_eb79_6be9_a5aa544851e0 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

test/custom-http-server.test.js lines 9–116

async function setup () {
  const localAddresses = await dns.lookup('localhost', { all: true })

  test('Should support a custom http server', { skip: localAddresses.length < 1 }, async t => {
    t.plan(5)

    const fastify = Fastify({
      serverFactory: (handler, opts) => {
        t.assert.ok(opts.serverFactory, 'it is called once for localhost')

        const server = http.createServer((req, res) => {
          req.custom = true
          handler(req, res)
        })

        return server
      }
    })

    t.after(() => fastify.close())
    fastify.get('/', (req, reply) => {
      t.assert.ok(req.raw.custom)
      reply.send({ hello: 'world' })
    })

    await fastify.listen({ port: 0 })

    const response = await fetch('http://localhost:' + fastify.server.address().port, {
      method: 'GET'
    })
    t.assert.ok(response.ok)
    t.assert.strictEqual(response.status, 200)
    const body = await response.text()
    t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
  })

  test('Should not allow forceCloseConnection=idle if the server does not support closeIdleConnections', t => {
    t.plan(1)

    t.assert.throws(
      () => {
        Fastify({
          forceCloseConnections: 'idle',
          serverFactory (handler, opts) {
            return {
              on () {

              }
            }
          }
        })
      },
      FST_ERR_FORCE_CLOSE_CONNECTIONS_IDLE_NOT_AVAILABLE,
      "Cannot set forceCloseConnections to 'idle' as your HTTP server does not support closeIdleConnections method"
    )
  })

  test('Should accept user defined serverFactory and ignore secondary server creation', async t => {
    const server = http.createServer(() => { })
    t.after(() => new Promise(resolve => server.close(resolve)))
    const app = Fastify({
      serverFactory: () => server
    })
    await t.assert.doesNotReject(async () => { await app.listen({ port: 0 }) })
  })

  test('Should not call close on the server if it has not created it', async t => {
    const server = http.createServer()

    const serverFactory = (handler, opts) => {
      server.on('request', handler)
      return server
    }

    const fastify = Fastify({ serverFactory })

    fastify.get('/', (req, reply) => {
      reply.send({ hello: 'world' })
    })

    await fastify.ready()

Domain

Subdomains

Frequently Asked Questions

What does setup() do?
setup() is a function in the fastify codebase, defined in test/custom-http-server.test.js.
Where is setup() defined?
setup() is defined in test/custom-http-server.test.js at line 9.

Analyze Your Own Codebase

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

Try Supermodel Free