* Apply the middlewares to the request. * * @param req * @param res * @param callback * @protected
(
req: IncomingMessage,
res: ServerResponse,
callback: (err?: any) => void,
)
| 370 | * @protected |
| 371 | */ |
| 372 | protected _applyMiddlewares( |
| 373 | req: IncomingMessage, |
| 374 | res: ServerResponse, |
| 375 | callback: (err?: any) => void, |
| 376 | ) { |
| 377 | if (this.middlewares.length === 0) { |
| 378 | debug("no middleware to apply, skipping"); |
| 379 | return callback(); |
| 380 | } |
| 381 | |
| 382 | const apply = (i) => { |
| 383 | debug("applying middleware n°%d", i + 1); |
| 384 | this.middlewares[i](req, res, (err?: any) => { |
| 385 | if (err) { |
| 386 | return callback(err); |
| 387 | } |
| 388 | |
| 389 | if (i + 1 < this.middlewares.length) { |
| 390 | apply(i + 1); |
| 391 | } else { |
| 392 | callback(); |
| 393 | } |
| 394 | }); |
| 395 | }; |
| 396 | |
| 397 | apply(0); |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Closes all clients. |