HTTP Handlers

This page summarizes how to properly use HTTP Handlers.

Please see Mandarine MVC Controllers before continuing.

How to properly use HTTP Handlers

HTTP handlers as we have mentioned before, are just methods that are decorated with route decoration. Although, they can handle complex operations by using HTTP Parameter decorators.

HTTP Parameter Decorators

They are decorators that are used in the parameters of a HTTP Handler. Every decorator provides a different functionality, but the objective is the same for all of them: The interaction of the current request & your mandarine-powered web application.

  • @RouteParam(name?: string)

    • It will bring the value of the param declared on the route path. This value comes from the client's request.

    • name is used to identify what parameter from the route path we are trying to get

      • Optional

      • If name is ignored, then its value will be the parameter's key.

  • @QueryParam(name?: string)

    • It will bring the value of the desired query parameter. This value comes from the URL a client has requested.

    • name is used to identify what query parameter from the request's url we are trying to get

      • Optional

      • If name is ignored, then its value will be the parameter's key.

  • @RequestBody()

    • It will bring the value from request's body.

      • It is the equivalent to request.body().

      • Mandarine will try to parse it when the content type of the body is application/x-www-form-urlencoded or application/json . If the content-type is not known, the whole body object will be inject it instead.

  • @Session()

    • It will bring the current session object located in the request.

  • @Cookie(name?: string)

    • It will bring the cookie with the key name

    • name is used to identify what cookie from the request we are trying to get

      • Optional

      • If name is ignored, then its value will be the parameter's key.

  • @ServerRequestParam()

    • It will bring the all the information available from the request made by a client.

  • @RequestParam()

    • It will bring the all the information available from the request made by a client.

      • The result is a Request object from Oak.

  • @ResponseParam()

    • It will bring the value from the current response given by the server before being delivered to the client.

    • Modifications to the request can and will affect the outcome of the response delivered to the client after the HTTP handler has been executed.

  • @Model()

    • It will inject the ViewModel object, used to create data models for templates. (More information here)

Usage

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

@Controller('/api')
export class Foo {
     
    @GET('/say-hi/:personsName')
    public httpHandler1(@RouteParam() personsName: string): string {
        return `Hello ${personsName}, welcome to Mandarine.TS`;
    }
    
    // This one specifies manually what parameter we are trying to get
    @GET('/say-hello/:name')
    public httpHandler2(@RouteParam('name') personsName: string): string {
        return `Hello ${personsName}, welcome to Mandarine.TS`;
    }

}

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

Request

# http://localhost:8080/api/say-hi/Bill
Hello Bill, Welcome to Mandarine.TS

# http://localhost:8080/api/say-hello/Mark
Hello Mark, Welcome to Mandarine.TS

Last updated