Skip to content

Commit 4b1fe2d

Browse files
committed
feat(server): add lifecycle hooks for custom logic execution in ExpressServer
1 parent 044b708 commit 4b1fe2d

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ const config = new ServerConfigBuilder()
6565

6666
const server = new ExpressServer(config, {
6767
beforeInit: srv => console.log('Initializing server...'),
68+
beforeRoutes: app => console.log('Setting up routes...'),
69+
afterRoutes: app => console.log('Routes initialized.'),
6870
afterInit: srv => console.log('Server initialized'),
71+
onServerCreated: srv => console.log('HTTP/HTTPS server instance created.'),
6972
beforeStart: app => console.log('Starting server...'),
7073
afterStart: srv => console.log('Server started!'),
7174
beforeStop: srv => console.log('Stopping server...'),

src/server/server.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ export class ExpressServer {
205205
// Set up middleware stack (order is critical)
206206
await this.setupMiddleware();
207207

208+
// Allow users to run custom logic before routes are registered (e.g. for adding global middleware, modifying app instance, etc.)
209+
await this.runHook('beforeRoutes', this.app);
210+
208211
// Set up default routes and error handling
209212
await this.setupRoutes();
210213

@@ -637,6 +640,9 @@ export class ExpressServer {
637640
const routerToUse = this.externalRouter || this.rootRouter;
638641
this.app.use(this.globalPrefix, routerToUse);
639642

643+
// Allow users to run custom logic after routes are registered but before error handling is set up
644+
await this.runHook('afterRoutes', this.app);
645+
640646
// 404 handler (must be after all other routes)
641647
this.app.use((req: Request, res: Response) => {
642648
const status = HttpStatusCodes.NOT_FOUND;
@@ -796,6 +802,7 @@ export class ExpressServer {
796802
};
797803

798804
this.server = this.createServerInstance(onListening);
805+
this.runHook('onServerCreated', this.server);
799806
this.setupConnectionTracking();
800807
this.setupServerErrorHandling(reject);
801808
} catch (error) {

src/types/server.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Express, json, NextFunction, urlencoded, Request, Response } from 'express';
22
import type http from 'node:http';
3+
import type https from 'node:https';
34
import type { ExpressServer } from '@catbee/utils/server';
45
import type { HelmetOptions } from 'helmet';
56
import type { CompressionOptions } from 'compression';
@@ -454,8 +455,14 @@ export interface CatbeeServerConfig {
454455
export interface CatbeeServerHooks {
455456
/** Called before middleware & routes initialize */
456457
beforeInit?: (server: ExpressServer) => Promise<void> | void;
458+
/** Called before any routes are registered */
459+
beforeRoutes?: (app: Express) => Promise<void> | void;
460+
/** Called after all routes are registered, before error handling middleware */
461+
afterRoutes?: (app: Express) => Promise<void> | void;
457462
/** Called after middleware & routes initialize */
458463
afterInit?: (server: ExpressServer) => Promise<void> | void;
464+
/** Called when the underlying HTTP/HTTPS server instance is created */
465+
onServerCreated?: (server: http.Server | https.Server) => Promise<void> | void;
459466
/** Called before server starts listening */
460467
beforeStart?: (app: Express) => Promise<void> | void;
461468
/** Called after server is ready */

0 commit comments

Comments
 (0)