# connect-cors

> CORS / XHR2 support for Node.JS's Connect

Latest version **0.5.6** (published 2013-09-23) · 0 weekly downloads

## Install

```sh
npm install connect-cors
pnpm add connect-cors
yarn add connect-cors
bun add connect-cors
```

## Health

**Score 10/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; low quality score; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.6 |
| Published | 2013-09-23 |
| First published | 2011-09-15 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 0.4.0 |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 26 |
| Author | Antono Vasiljev |
| Maintainers | antono |

## Links

- npm: https://www.npmjs.com/package/connect-cors
- Repository: https://github.com/antono/connect-cors
- Issues: https://github.com/antono/connect-cors/issues
- npm.io page: https://npm.io/package/connect-cors

## Dependencies (1)

- [connect](https://npm.io/package/connect.md) >= 1.6.4

## Recent versions

- 0.5.6 (latest) — 2013-09-23
- 0.5.5 — 2013-09-23
- 0.5.1 — 2012-05-24
- 0.0.1 — 2011-09-15
- 0.0.2 — 2011-09-15
- 0.5.0 — 2011-09-15

## README

# Connect CORS


A CORS module for Node.JS's Connect
"pre-flighted" OPTIONS requests are supported


## Installation

    npm install connect-cors


## Usage


    var Connect = require('connect')
      , CORS = require('connect-cors')
      , options = {}
      , server
      ;
     
    server = Connect.createServer(
        // uses reasonable defaults when no options are given
        CORS(options)
      , function(req, res) {
          res.writeHead(200, { 'Content-Type': 'text/plain' });
          res.end('Hello World');
        }
    );

    // the `options` object will be popullated with empty arrays
    // and is live-editable (great for testing and dynamic APIs)
    console.log(options);

    server.listen(9000);

**Known Issues**

  * "pre-flighted" OPTIONS requests and "actual" requests are treated the same. This doesn't seem to have any adverse affects, it just wastes bytes.


## Options

These are the default options when no options are specified.

The options are hot-editable. You can dynamically add origins, resources, etc.

    var options = {
            origins: []                       // implicit same as ['*'], and null
          , methods: ['HEAD', 'GET', 'POST']  // OPTIONS is always allowed
          , headers: [                        // both `Exposed` and `Allowed` headers
                'X-Requested-With'
              , 'X-HTTP-Method-Override'
              , 'Content-Type'
              , 'Accept'
            ]
          , credentials: false                // don't allow Credentials
          , resources: [
              {
                  pattern: '/'                // a string prefix or RegExp
              //, origins
              //, methods
              //, headers
              //, credentials
              }
            ]
        };


#### Explanation


By default the defaults will be used.

If the top-level options are present, they will be used for any resource which does not specify its own

Resource-level directives override top-level directives

#### origins

an array of origins. `undefined`, `null`, `[]`, and `['*']` will all default to '*'

  * Example: `['http://example.com', 'http://domain.tld']`
  * Browsers will see their origin exactly `Access-Control-Allow-Origin: http://example.com` (even if ['*'] is used)
  * MSIE will see `Access-Control-Allow-Origin: *`, for the allowed origins. (`withCredentials` is broken in MSIE)

#### methods

any HTTP verb will do

#### headers

used for both `Access-Control-Exposed-Headers` and `Access-Contral-Allowed-Headers`

  * TODO make those separate


#### credentials

allows XHR2 clients to sepecify `withCredentials = true`, which will send `Cookies` and `HTTP Basic Auth`

  * broken for XDR in MSIE

#### resources

an array of "directive" objects

#### pattern

  * pattern (string prefix) - '/path/to' will match '/path/too...', '/path/to/res...', but NOT '/some/path/to...'
  * pattern (RegExp) - use your own regex matching (to your own peril)
    * Please see [http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex/6969486#6969486](Escape string for use in Javascript regex) to avoid ugly bugs when creating dynamic regex for paths

## Tests

See the /test folder. There are some example servers and some basic tests.

## License 

(The MIT License)

Copyright (c) 2010 Antono Vasiljev

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

---
_Source: https://npm.io/package/connect-cors · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
