(opts: ConfigureServerOpts)
| 183 | * @param opts - Configuration options |
| 184 | */ |
| 185 | export function configureServer(opts: ConfigureServerOpts): void { |
| 186 | const { |
| 187 | app, |
| 188 | addRoutes, |
| 189 | allowCors = false, |
| 190 | basePath: rawBasePath = DEFAULT_BASE_PATH, |
| 191 | extraMethodMap = {}, |
| 192 | webSocketsMapping = {}, |
| 193 | useLegacyUpgradeHandler = true, |
| 194 | frontRouter, |
| 195 | } = opts; |
| 196 | const basePath = normalizeBasePath(rawBasePath); |
| 197 | |
| 198 | app.use(endLogFormatter); |
| 199 | app.use(handleLogContext); |
| 200 | // Apply before parsing, routing, and idempotent replay so error and proxy responses are covered too. |
| 201 | app.use((_req, res, next) => { |
| 202 | res.setHeader('Cache-Control', 'no-cache'); |
| 203 | next(); |
| 204 | }); |
| 205 | |
| 206 | // Only use legacy Express middleware for WebSocket upgrades if shouldUpgradeCallback is not available. |
| 207 | // When shouldUpgradeCallback is available, upgrades are handled directly on the HTTP server |
| 208 | // to avoid Express middleware timeout issues with long-lived connections |
| 209 | if (useLegacyUpgradeHandler) { |
| 210 | app.use(handleUpgrade(webSocketsMapping, allowCors)); |
| 211 | } |
| 212 | app.use(allowCors ? allowCrossDomain : rejectBrowserOrigin); |
| 213 | app.use(handleIdempotency); |
| 214 | app.use(defaultToJSONContentType); |
| 215 | app.use(bodyParser.urlencoded({extended: true})); |
| 216 | app.use(methodOverride()); |
| 217 | |
| 218 | app.use(parseJsonBody); |
| 219 | |
| 220 | // set up start logging (which depends on bodyParser doing its thing) |
| 221 | app.use(startLogFormatter); |
| 222 | |
| 223 | // mounted before any route, so middleware added later (e.g. via `updateServer`) still runs first |
| 224 | app.use(frontRouter); |
| 225 | |
| 226 | addRoutes(app, {basePath, extraMethodMap}); |
| 227 | |
| 228 | // Express only passes errors to an error handler registered after the middleware |
| 229 | // that raised them, so this one must be the last of all. |
| 230 | app.use(catchAllHandler); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Normalize base path string (leading slash, no trailing slash). |
no test coverage detected