Middleware call sequences

As you see in the previous section, a middleware can be use on different context:

Middleware associated to a controller or endpoint as a same constraint that an endpoint. It'll be played only when the url request match with the path associated to the controller and his endpoint method.

When a request is sent to the server all middlewares added in the , Controller or Endpoint with decorators will be called while a response isn't sent by one of the middleware in the stack.

Note

The middlewares shown in the Endpoints box will be replayed as many times as it has endpoint that match the request url.

(1) Render middleware is called only when a the decorator is used on the endpoint.

(2) SendResponse middleware send a response only when a data is return by the endpoint method or if the endpoint is the latest called endpoint for the resolved route.

For example:

import {Controller, Get, Next, Use, UseAfter, UseBefore, UseBeforeEach} from "@tsed/common";

@Controller("/")
@UseAfter(MdlwCtrlAfter)
@UseBefore(MdlwCtrlBefore)
@UseBeforeEach(MdlwCtrlBeforeEach)
@Use(MdlwCtrl)
export class MyCtrl {

  @Get("/")
  @UseBefore(MdlwBefore)
  @Use(Mdlw)
  @UseAfter(MdlwAfter)
  endpointA(@Next() next: Next) {
    console.log("EndpointA");
    next();
  }

  @Get("/")
  endpointB() {
    console.log("EndpointB");

    return {};
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

According to the call sequence scheme, the stack calls will be there:

  • Middlewares added in ServerLoader (logger, express middleware, etc...),
  • MdlwCtrlBefore,
  • MdlwCtrlBeforeEach
  • MdlwBefore,
  • MdlwCtrl,
  • MyCtrl.endpointA,
  • MdlwAfter,
  • SendResponse, (but nothing data is returned by the endpointA)
  • MdlwCtrlBeforeEach
  • MdlwCtrl,
  • MyCtrl.endpointB,
  • MdlwAfter,
  • SendResponse, send a response because endpointB return a data,
  • MdlwCtrlAfter, but this middleware will not be called because a response is sent.
  • Middleware added in ServerLoader (not called too).