Mandarine.TS
Latest
Latest
  • Mandarine.TS
  • Getting started
    • Installing Deno Runtime
    • Setting up Environment
    • Quick Example
  • Concepts
  • Mandarine Project Structure
  • Mandarine CLI
    • CLI Reference
      • mandarine new
      • mandarine generate
      • mandarine run
  • Mandarine Core
    • Core Initialization
    • Mandarine.json
    • Dot env file
    • Properties
      • The @Value Decorator
    • Components
      • Controller
      • Middleware
      • Repository
      • Component
      • Service
      • Configuration
      • Manual Component
    • Dependency Injection
      • Accessing DI container
    • Resource Handlers
      • Resource Handler Registry
      • Resource Handler
      • Resource Resolver
  • Mandarine MVC
    • Web MVC
    • Controllers
      • Routes
        • Parameterized Routes
      • HTTP Handlers
      • @ResponseStatus
    • Custom Middleware
    • Session Middleware
    • CORS Middleware
    • Template Engine
      • @Render Decorator
      • Accessing data from template
    • Launching web-app
    • Serving Static Content
    • Multipart Form Data
  • Mandarine Security
    • Sessions
  • Mandarine Data
    • ORM
      • Data source
      • Models
      • Repositories
        • Interacting with data
        • Updating data
    • Mandarine Query Language
  • Resources
    • Changelog
  • Plugins
    • Optional
    • Promise Repeater
Powered by GitBook
On this page
  • Main
  • Usage

Was this helpful?

  1. Mandarine MVC

Custom Middleware

Previous@ResponseStatusNextSession Middleware

Last updated 4 years ago

Was this helpful?

For this article, it is necessary to have a understanding of , , &

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 .

contains two key methods which you will need to provide functionality.

  • onPreRequest(...args): boolean

    • It is executed before the request reaches to the .

    • 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 .

Usage

Syntax:

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()");
    }
}
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";
    }

}
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();
deno run --config tsconfig.json --allow-net --allow-read index.ts

Result

# http://localhost:8080/api/hello-world
[Console] pre-request()
Hello world
[Console] post-request()
Middleware component
Controllers
Routes
HTTP Handlers
MiddlewareTarget
MiddlewareTarget
HTTP Handler
HTTP parameter decorators
See syntax and extra information here