Class FastHTML

It's common for an app to show an HTML view when no route was able to handle a request, so the idea behind this middleware is to avoid going to every middleware and controller and just specify that if the request is not for a route handled by a controller, just serve the HTML and avoid processing unnecessary data.

A simple example: The app has a route /backend that a frontend uses to get information. This middleware can be used to only allow the execution of middlewares and controllers when the request route is for /backend.

Disclaimer: Managing statics files with Express is not a best practice, but there are scenarios where there is not other choice.

Constructors

Properties

_events: Events

The application envent bus, to listen and get the list of "controlled routes" after the application is started.

_fileReady: boolean = false

Whether or not the file is ready to be served. In case the middleware uses an HTMLGenerator service, the file needs to be generated before being available, and that's why this flag exists.

_getHTMLGenerator: (() => undefined | HTMLGenerator)

A function to get a possible HTMLGenerator. This is injected as a "getter" to not interrupt the DIC "lifecycle": middlewares are initialized right when the app starts, and injecting a reference would force the service to be initialized too, even if a request is not being made.

_options: FastHTMLOptions

The customization options for the middleware.

_routeExpressions: RegExp[] = []

A list of regular expression that match the routes controlled by the application. This is in case the useAppRoutes option is set to true; when the application gets started, an event listener will obtain all the top controlled routes, create regular expressions, and save them on this property.

_sendFile: SendFile

The service that serves a file.

Accessors

Methods

  • Serves the HTML file to the response.

    Parameters

    • res: Response<any, Record<string, any>>

      The response object generated by the application.

    • next: NextFunction

      The function to call the next middleware.

    Returns void

  • Adds the event listener that obtains the list of "controlled routes" when useAppRoutes is set to true.

    Returns void

  • Validates whether a route should be ignored or not. The method checks first against the ignore option, and then against the list of "controlled routes" if useAppRoutes is set to true.

    Parameters

    • route: string

      The route to validate.

    Returns boolean