Socket
Socket
Sign inDemoInstall

corsen

Package Overview
Dependencies
1
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    corsen

A universal Cross-Origin Resource Sharing(CORS) middleware


Version published
Maintainers
1
Install size
20.6 kB
Created

Readme

Source

corsen

NPM version build status npm download

A universal Cross-Origin Resource Sharing(CORS) middleware. Derrived from @koa/cors.

Installation

npm install corsen

or

yarn add corsen

Quick start

Enable cors with default options:

  • origin: request Origin header
  • allowMethods: GET,HEAD,PUT,POST,DELETE,PATCH
const http = require('http')
const cors = require('corsen')({
  // place your options here
})

const server = http.createServer((req, res) => {
  cors(req, res)
  // if you pass a function to options.origin and that function returns a Promise, you can use async/await: await cors(req, res)
  res.writeHead(200, { 'Content-Type': 'text/plain' })
  res.end('ok')
})

cors(options)

/**
 * CORS middleware
 *
 * @param {Object} [options]
 *  - {String|Function(req, res)} origin `access-control-allow-origin`, default is request Origin header
 *  - {String|Array} allowMethods `access-control-allow-methods`, default is 'GET,HEAD,PUT,POST,DELETE,PATCH'
 *  - {String|Array} exposeHeaders `access-control-expose-headers`
 *  - {String|Array} allowHeaders `access-control-allow-headers`
 *  - {String|Number} maxAge `access-control-max-age` in seconds
 *  - {Boolean} credentials `access-control-allow-credentials`
 * @return {Function} cors middleware
 * @api public
 */

Credit

All the commit before 71c4d00 are credited to koajs contributors.

Difference between corsen and @koa/cors

  • The middleware function returned by corsen has a signature of function(http.IncomingMessage, http.ServerResponse), while @koa/cors recieves a koa's Context object.
  • All the header names set in corsen are lowercase.
  • corsen has removed the error handling utility of @koa/cors. There is not keepHeadersOnError porperty in options.

Examples

felid-cors A Felid plugin for CORS.

License

MIT

Keywords

FAQs

Last updated on 17 Nov 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc