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

Function createServer

lib/server.js:27–149  ·  lib/server.js::createServer
(options, httpHandler)

Source from the content-addressed store, hash-verified

25}
26
27function createServer (options, httpHandler) {
28 const server = getServerInstance(options, httpHandler)
29
30 class="cm">// `this` is the Fastify object
31 function listen (
32 listenOptions = { port: 0, host: class="st">'localhost' },
33 cb = undefined
34 ) {
35 if (typeof cb === class="st">'function') {
36 if (cb.constructor.name === class="st">'AsyncFunction') {
37 FSTWRN003(class="st">'listen method')
38 }
39
40 listenOptions.cb = cb
41 }
42 if (listenOptions.signal) {
43 if (typeof listenOptions.signal.on !== class="st">'function' && typeof listenOptions.signal.addEventListener !== class="st">'function') {
44 throw new FST_ERR_LISTEN_OPTIONS_INVALID(class="st">'Invalid options.signal')
45 }
46
47 class="cm">// 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(class="st">'abort', onAborted, { once: true })
58 }
59 }
60
61 class="cm">// If we have a path specified, donclass="st">'t default host to 'localhostclass="st">' so we don't end up listening
62 class="cm">// on both path and host
63 class="cm">// See https://github.com/fastify/fastify/issues/4007
64 let host
65 if (listenOptions.path == null) {
66 host = listenOptions.host ?? class="st">'localhost'
67 } else {
68 host = listenOptions.host
69 }
70 if (!Object.hasOwn(listenOptions, class="st">'host') ||
71 listenOptions.host == null) {
72 listenOptions.host = host
73 }
74 if (host === class="st">'localhost') {
75 listenOptions.cb = (err, address) => {
76 if (err) {
77 class="cm">// 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