LogoLogo
ChangelogGitHubTwitterGitter
v2.x
v2.x
  • Introduction
  • Overview
    • Getting started
    • Effects
    • Routing
    • Middlewares
    • Error handling
    • How does it glue​ together?
  • Advanced
    • Context
    • Server events
    • Validation
    • Streaming
    • Output interceptor
  • WebSockets
    • Getting started
    • Effects
    • Middlewares
    • Error handling
    • Connections handling
  • API Reference
    • core
      • bindTo
      • createServer
      • combineRoutes
      • createContextToken
      • EffectFactory
      • r.pipe
      • httpListener
      • operator: matchEvent
      • operator: use
    • websockets
      • webSocketListener
      • operator: broadcast
      • operator: mapToServer
    • middleware-body
    • middleware-logger
    • middleware-io
    • middleware-jwt
      • Token signing
    • middleware-joi
    • middleware-cors
    • middleware-multipart
  • Other
    • Migration from version 1.x
    • Changelog
    • FAQ
Powered by GitBook
On this page
  • Installation
  • Importing
  • Type declaration
  • Parameters
  • Basic usage
  • Strict usage
  1. API Reference

middleware-cors

A CORS middleware for Marble.js

Installation

$ npm i @marblejs/middleware-cors

Requires @marblejs/core to be installed.

Importing

import { cors$ } from '@marblejs/middleware-cors';

Type declaration

cors$ :: CORSOptions -> HttpMiddlewareEffect

Parameters

parameter

definition

options

<optional> CORSOptions

CORSOptions

parameter

definition

origin

<optional> string | string[] | RegExp

methods

<optional> HttpMethod[]

optionsSuccessStatus

<optional> HttpStatus

allowHeaders

<optional> string | string[]

exposeHeaders

<optional> string[]

withCredentials

<optional> boolean

maxAge

<optional> number

This object allows you to configure CORS headers with various options. Both methods and exposeHeaders support wildcard. By default options are configured as following.

{ 
  origin: '*',
  methods: ['HEAD', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
  withCredentials: false,
  optionsSuccessStatus: HttpStatus.NO_CONTENT, // 204
}

Note that provided options are merged with default options so you need to overwrite each default parameter you want to customize.

Basic usage

app.ts
import { cors$ } from '@marblejs/middleware-cors';

export default httpListener({
  middlewares: [
    cors$({
      origin: '*',
      allowHeaders: '*',
      methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
    })
  ],
  effects: [/* ... */],
});

For security purpose it's better to be strict as possible when configuring CORS options.

Strict usage

app.ts
import { cors$ } from '@marblejs/middleware-cors';

export default httpListener({
  middlewares: [
    cors$({
      origin: ['http://example1.com', 'http://example2.com'],
      allowHeaders: ['Origin', 'Authorization', 'Content-Type'],
      methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
    })
  ],
  effects: [/* ... */],
});

Headers notation is case insensitive. content-type will also work.

Previousmiddleware-joiNextmiddleware-multipart

Last updated 6 years ago