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
  • Basic Usage

Was this helpful?

  1. Mandarine MVC

CORS Middleware

PreviousSession MiddlewareNextTemplate Engine

Last updated 4 years ago

Was this helpful?

Main

The CORS middleware allows you to enable or disable . This way, you can protect your endpoints from being requested from unknown origins or undesired origins.

Usage

The usage of CORS middleware is quiet similar to other Mandarine modules, it is done through the use of Decorators.

Syntax:

@Cors(corsOptions: )

Interface (.):

  • origin

    • Declares the valid origins. It can be a string, a RegExp, or an array of both string & RegExp for multiple origins.

    • Default: *

  • methods

    • Methods allowed

    • Default: ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE"]

  • allowedHeaders

    • List of headers to be added to access-control-request-headers

  • exposedHeaders

    • List of headers to be added to accessl-control-expose-headers

  • credentials

    • Boolean value for header access-control-allow-credentials

  • maxAge

    • Value for header access-control-max-age

  • optionsSuccessStatus

    • Http response code for when CORS has been accepted.

    • Default: 204

Basic Usage

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

@Controller('/api')
@Cors({ origin: "https://myorigin.com" })
export class MyApi {

    @GET('/hello')
    public handler() {
        return "hello";
    }

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

@Controller('/api')
export class MyApi {

    @GET('/hello')
    @Cors({ origin: "https://myorigin.com" })
    public handler() {
        return "hello";
    }
    
}

At level:

At level

Cross-origin resource sharing
Mandarine.MandarineMVC.CorsMiddlewareOption
MandarineMvc
CorsMiddlewareOption
Controller
Route