1.1.2 • Published 8 years ago

@starefossen/express-cors v1.1.2

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

@starefossen/express-cors

Build status Codacy grade Codacy coverage NPM downloads NPM version Node version Dependency status

Smart CORS headers middleware for your Express.js applications.

Install

$ npm install @starefossen/express-cors --save

Usage

const cors = require('@starefossen/express-cors');

Simple

The simple configuration is controlled by environment variables:

VariableDescriptionDefault
CORS_ALLOW_CREDENTIALSallow-credentials (boolean)undefined
CORS_ALLOW_HEADERSallow-headers header (comma separated string)Content-Type
CORS_ALLOW_METHODSallow-methods header (comma separated string)GET, OPTIONS
CORS_ALLOW_ORIGINSorins whitelist (comma seperated string)""
CORS_DENY_ORIGINSorigins blacklist (comma seperated string)""
CORS_EXPOSE_HEADERSexpose-headers header (comma seperated string)""
CORS_MAX_AGEmax-age header (integer)0
CORS_REQUIRE_ORIGINrequire origin header from client (boolean)false
const cors = require('@starefossen/express-cors');

app.use(cors.middleware);

Advanced

The advanced configuration takes in a configuration object. All values defaults to their environment conunterpart as statated in the simple configuration.

const cors = require('@starefossen/express-cors');

app.use(cors({
  allowCredentials: false,
  allowHeaders: 'Content-Type',
  allowMethods: 'GET, OPTIONS',
  allowOrigins: 'foo.com,bar.com',
  denyOrigins: 'example.com',
  exposeHeaders: 'x-request-time',
  maxAge: 133734,
  requireOrigin: true,
});

Localhost

If you want to allow local development domains like localhost or file:// you can add the following to your CORS_ALLOW_ORIGINS environment variable:

CORS_ALLOW_ORIGINS='localhost,null'

MIT Licensed