The options to construct the class.
Protected Readonly_The application event bus, to listen and get the list of "controlled routes" after the application is started.
Protected_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.
Protected Readonly_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.
Protected_The customization options for the middleware.
Protected_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.
Protected Readonly_The service that serves a file.
The customization options.
Protected_Serves the HTML file to the response.
The response object generated by the application.
The function to call the next middleware.
Protected_Adds the event listener that obtains the list of "controlled routes" when
useAppRoutes is set to true.
Protected_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.
The route to validate.
Generates the middleware that serves the HTML file.
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
/backendthat 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.
Prettierignore