> For the complete documentation index, see [llms.txt](https://mandarineframework.gitbook.io/mandarine-ts/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mandarineframework.gitbook.io/mandarine-ts/mandarine-mvc/custom-middleware.md).

# Custom Middleware

{% hint style="info" %}
For this article, it is necessary to have a understanding of [Middleware component](https://mandarineframework.gitbook.io/mandarine-ts/mandarine-core/components/middleware), [Controllers](https://mandarineframework.gitbook.io/mandarine-ts/mandarine-mvc/controllers), [Routes](https://mandarineframework.gitbook.io/mandarine-ts/mandarine-mvc/controllers/using-routes-and-http-handlers) & [HTTP Handlers](https://mandarineframework.gitbook.io/mandarine-ts/mandarine-mvc/controllers/http-handlers)
{% endhint %}

## Main

Custom middleware are used to provide interceptions to certain endpoints in your Application. The use cases may vary, but the most common use cases are verifications.

In order for a middleware to be considered a *mandarine middleware,* your middleware class needs to **implement** the class [MiddlewareTarget](https://doc.deno.land/https/raw.githubusercontent.com/mandarineorg/mandarinets/master/main-core/components/middleware-component/middlewareTarget.ts).

[MiddlewareTarget](https://doc.deno.land/https/raw.githubusercontent.com/mandarineorg/mandarinets/master/main-core/components/middleware-component/middlewareTarget.ts) contains two key methods which you will need to provide functionality.

* `onPreRequest(...args): boolean`
  * It is executed before the request reaches to the [HTTP Handler](https://mandarineframework.gitbook.io/mandarine-ts/mandarine-mvc/controllers/http-handlers).&#x20;
  * If it returns **true,** the request is authorized to continue to the *HTTP handler* & then to *the post-request handler*.
  * If it returns **false**, the request will be stopped and the *HTTP handler* nor the *post-request handler* will  be reached.
* `onPostRequest(...args): void`
  * It is executed after the request has reached & called the *HTTP handler.*

**Note** that both `onPreRequest` and `onPostRequest` **can** absolutely use all [HTTP parameter decorators](https://mandarineframework.gitbook.io/mandarine-ts/mandarine-mvc/controllers/http-handlers#http-parameter-decorators).

## Usage

**Syntax:** [See syntax and extra information here](https://mandarineframework.gitbook.io/mandarine-ts/mandarine-core/components/middleware#usage)

```typescript
middleware.ts

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

@Middleware(new RegExp('/api/*'))
export class Middleware1 implements MiddlewareTarget {

    public onPreRequest(@ResponseParam() response: any): boolean {
        console.log("pre-request()");
        return true;
    }
    
    public onPostRequest(): void {
        console.log("post-request()");
    }
}
```

```typescript
controller.ts

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

@Controller()
export class MyController {

    @GET('/api/hello-world')
    public helloWorldApi() {
        return "Hello world";
    }

}
```

{% tabs %}
{% tab title="Index file" %}

```typescript
import { MandarineCore } from "https://deno.land/x/mandarinets/mod.ts";

import { MyController } from "./controller.ts";
import { Middleware1 } from "./middleware.ts";

const controllers = [MyController1];
const middleware = [Middleware1];

new MandarineCore().MVC().run();
```

{% endtab %}

{% tab title="Deno execution" %}

```bash
deno run --config tsconfig.json --allow-net --allow-read index.ts
```

{% endtab %}
{% endtabs %}

Result

```bash
# http://localhost:8080/api/hello-world
[Console] pre-request()
Hello world
[Console] post-request()
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://mandarineframework.gitbook.io/mandarine-ts/mandarine-mvc/custom-middleware.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
