MCPcopy
hub / github.com/fastify/fastify / createServer

Function createServer

lib/server.js:27–149  ·  view source on GitHub ↗
(options, httpHandler)

Source from the content-addressed store, hash-verified

25}
26
27function createServer (options, httpHandler) {
28 const server = getServerInstance(options, httpHandler)
29
30 // `this` is the Fastify object
31 function listen (
32 listenOptions = { port: 0, host: 'localhost' },
33 cb = undefined
34 ) {
35 if (typeof cb === 'function') {
36 if (cb.constructor.name === 'AsyncFunction') {
37 FSTWRN003('listen method')
38 }
39
40 listenOptions.cb = cb
41 }
42 if (listenOptions.signal) {
43 if (typeof listenOptions.signal.on !== 'function' && typeof listenOptions.signal.addEventListener !== 'function') {
44 throw new FST_ERR_LISTEN_OPTIONS_INVALID('Invalid options.signal')
45 }
46
47 // copy the current signal state
48 this[kState].aborted = listenOptions.signal.aborted
49
50 if (this[kState].aborted) {
51 return this.close()
52 } else {
53 const onAborted = () => {
54 this[kState].aborted = true
55 this.close()
56 }
57 listenOptions.signal.addEventListener('abort', onAborted, { once: true })
58 }
59 }
60
61 // If we have a path specified, don't default host to 'localhost' so we don't end up listening
62 // on both path and host
63 // See https://github.com/fastify/fastify/issues/4007
64 let host
65 if (listenOptions.path == null) {
66 host = listenOptions.host ?? 'localhost'
67 } else {
68 host = listenOptions.host
69 }
70 if (!Object.hasOwn(listenOptions, 'host') ||
71 listenOptions.host == null) {
72 listenOptions.host = host
73 }
74 if (host === 'localhost') {
75 listenOptions.cb = (err, address) => {
76 if (err) {
77 // the server did not start
78 cb(err, address)
79 return
80 }
81
82 multipleBindings.call(this, server, httpHandler, options, listenOptions, () => {
83 this[kState].listening = true
84 cb(null, address)

Callers 2

fastifyFunction · 0.85
server.test.jsFile · 0.85

Calls 1

getServerInstanceFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…