Resource Handlers

This article requires extensive knowledge of Components & Manual Components

Main

Resource handlers are a way to intercept and resolve a specific resources requested by a client. They work as a form of middleware interceptor.

A good example of a resource handler is the way mandarine handles static content. Internally, Mandarine has a Resource Handler that will get the requested file based on the URL of the request. (See source code here)

Overriding Behavior

It is possible to create your own resource handlers & resource resolvers. For this, Mandarine allows you to implement Mandarine.MandarineMVC.Configurers.WebMVCConfigurer which will override the default behavior established by Mandarine.

In order to override this behavior, create a component (Configuration component is recommended). This component must implement Mandarine.MandarineMVC.Configurers.WebMVCConfigurer .

import { Mandarine, Configuration } from "https://deno.land/x/mandarinets/mod.ts";

@Configuration()
export class WebMVCConfigurer implements Mandarine.MandarineMVC.Configurers.WebMVCConfigurer {

}

Then, we need to override the method addResourceHandlers , which we will also need to decorate with @Injectable() .

import { Mandarine, Configuration } from "https://deno.land/x/mandarinets/mod.ts";

@Configuration()
export class WebMVCConfigurer implements Mandarine.MandarineMVC.Configurers.WebMVCConfigurer {

    @Injectable()
    public addResourceHandlers(): Mandarine.MandarineCore.IResourceHandlerRegistry {
    }
    
}

Please refer to Resource Handler & Resource Handler Registry before continuing.

Our method addResourceHandlers will return an instance of the Resource Handler Registry with our resource handlers now added.

In the example above, we are adding a resource handler that will be executed every time our client requests "/css" or "/js". See a better explanation of this here.

Last updated

Was this helpful?