# CORS Middleware

## Main

The CORS middleware allows you to enable or disable [Cross-origin resource sharing](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing). 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:` [`Mandarine.MandarineMVC.CorsMiddlewareOption`](https://doc.deno.land/https/raw.githubusercontent.com/mandarineorg/mandarinets/master/mvc-framework/mandarine-mvc.ns.ts#MandarineMvc.CorsMiddlewareOption)`)`

**Interface (**[MandarineMvc](https://doc.deno.land/https/raw.githubusercontent.com/mandarineorg/mandarinets/master/mvc-framework/mandarine-mvc.ns.ts#MandarineMvc).[CorsMiddlewareOption](https://doc.deno.land/https/raw.githubusercontent.com/mandarineorg/mandarinets/master/mvc-framework/mandarine-mvc.ns.ts#MandarineMvc.CorsMiddlewareOption))**:**

* `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

**At** [**Controller**](https://mandarineframework.gitbook.io/mandarine-ts/mandarine-mvc/controllers) **level:**

```typescript
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";
    }

}
```

**At** [**Route**](https://mandarineframework.gitbook.io/mandarine-ts/mandarine-mvc/controllers/using-routes-and-http-handlers) **level**

```typescript
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";
    }
    
}
```


---

# Agent Instructions: 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:

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

The question should be specific, self-contained, and written in natural language.
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.
