Giter Club home page Giter Club logo

universal-cors's Introduction

universal-cors

CORS middleware implementation with emphasis on configurability of dynamic origins.

Build Status codecov

NPM

Install

npm i -S universal-cors

How to use

import express from 'express'
import cors, { origins } from 'universal-cors'


const app = express()

/** cors middleware to accept any pattern matching example.com subdomains */
app.use(cors({ patterns: [ /^https:\/\/.*\.example\.com/ ]}))

/** ROUTERS GO HERE */

Documentation

cors default export - middleware for auto handling preflight responses, testing dynamic origins, and attaching cors response headers when valid request occurs

cors([opts: Object]): function(req, res, next)

opts

name type default description
patterns `string RegExp Array<string
preflight function(req): responseHeaders (req) => {} issues preflight responses for OPTIONS method requests and returns specified headers
tracing boolean false toggles tracing for debugging purposes
logger Object console the logger object to trace to
loglevel string 'info' the log level to use when tracing (error, warn, info, trace)

An example of what you might set for preflight:

const preflight = req => {
  return  { 'Access-Control-Allow-Origin': req.headers.origin
          , 'Access-Control-Max-Age': 604800 // Specifies how long the preflight response can be cached in seconds
          , 'Access-Control-Allow-Methods': 'GET, PUT, POST, DELETE'
          , 'Access-Control-Allow-Headers': 'Content-Type, Authorization'
          , 'Access-Control-Allow-Credentials': true
          }
}

origins export - granular origin testing functionality

origins([opts: Object]): { isOk: function(domain: string): boolean }

opts

name type default description
patterns `string RegExp Array<string
tracing boolean false toggles tracing for debugging purposes
logger Object console the logger object to trace to
loglevel string 'info' the log level to use when tracing (error, warn, info, trace)

universal-cors's People

Contributors

cchamberlain avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.