MCPcopy
hub / github.com/fastify/fastify / routeHandler

Function routeHandler

lib/route.js:463–591  ·  lib/route.js::routeHandler
(req, res, params, context, query)

Source from the content-addressed store, hash-verified

461
462 class="cm">// HTTP request entry point, the routing has already been executed
463 function routeHandler (req, res, params, context, query) {
464 const id = getGenReqId(context.server, req)
465
466 const loggerOpts = {
467 level: context.logLevel
468 }
469
470 if (context.logSerializers) {
471 loggerOpts.serializers = context.logSerializers
472 }
473 const childLogger = createChildLogger(context, logger, req, id, loggerOpts)
474 class="cm">// Set initial value; will be re-evaluated after FastifyRequest is constructed if it's a function
475 childLogger[kDisableRequestLogging] = disableRequestLoggingFn ? false : disableRequestLogging
476
477 if (closing === true) {
478 /* istanbul ignore next mac, windows */
479 if (req.httpVersionMajor !== 2) {
480 res.setHeader(class="st">'Connection', class="st">'close')
481 }
482
483 class="cm">// Load-shedding fast path during drain. server.close() and
484 class="cm">// closeIdleConnections() only reap idle sockets; requests already
485 class="cm">// pipelined or arriving on an active keep-alive connection still reach
486 class="cm">// this point with closing === true. Short-circuiting with 503 avoids
487 class="cm">// running the full handler chain (and any downstream calls) for work
488 class="cm">// the load balancer should redirect elsewhere.
489 if (return503OnClosing) {
490 const headers = {
491 class="st">'Content-Type': class="st">'application/json',
492 class="st">'Content-Length': class="st">'80'
493 }
494 res.writeHead(503, headers)
495 res.end(class="st">'{"error":"Service Unavailable","message":"Service Unavailable","statusCode":503}')
496 childLogger.info({ res: { statusCode: 503 } }, class="st">'request aborted - refusing to accept new requests as server is closing')
497 return
498 }
499 }
500
501 class="cm">// When server.forceCloseConnections is true, we will collect any requests
502 class="cm">// that have indicated they want persistence so that they can be reaped
503 class="cm">// on server close. Otherwise, the container is a noop container.
504 const connHeader = String.prototype.toLowerCase.call(req.headers.connection || class="st">'')
505 if (connHeader === class="st">'keep-alive') {
506 if (keepAliveConnections.has(req.socket) === false) {
507 keepAliveConnections.add(req.socket)
508 req.socket.on(class="st">'close', removeTrackedSocket.bind({ keepAliveConnections, socket: req.socket }))
509 }
510 }
511
512 class="cm">// we revert the changes in defaultRoute
513 if (req.headers[kRequestAcceptVersion] !== undefined) {
514 req.headers[class="st">'accept-version'] = req.headers[kRequestAcceptVersion]
515 req.headers[kRequestAcceptVersion] = undefined
516 }
517
518 const request = new context.Request(id, params, req, query, childLogger, context)
519 const reply = new context.Reply(res, request, childLogger)
520

Callers

nothing calls this directly

Calls 8

getGenReqIdFunction · 0.85
createChildLoggerFunction · 0.85
setupResponseListenersFunction · 0.85
runPreParsingFunction · 0.85
onRequestAbortHookRunnerFunction · 0.85
infoMethod · 0.80
addMethod · 0.80
sendMethod · 0.80

Tested by

no test coverage detected