Dieses Repository beinhaltet HTML- und Javascript Code zur einer NotizenWebApp auf Basis von Web Storage. Zudem sind Mocha/Chai Tests im Browser enthalten. https://meinenotizen.netlify.app/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 9.0KB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. # cors
  2. [![NPM Version][npm-image]][npm-url]
  3. [![NPM Downloads][downloads-image]][downloads-url]
  4. [![Build Status][travis-image]][travis-url]
  5. [![Test Coverage][coveralls-image]][coveralls-url]
  6. CORS is a node.js package for providing a [Connect](http://www.senchalabs.org/connect/)/[Express](http://expressjs.com/) middleware that can be used to enable [CORS](http://en.wikipedia.org/wiki/Cross-origin_resource_sharing) with various options.
  7. **[Follow me (@troygoode) on Twitter!](https://twitter.com/intent/user?screen_name=troygoode)**
  8. * [Installation](#installation)
  9. * [Usage](#usage)
  10. * [Simple Usage](#simple-usage-enable-all-cors-requests)
  11. * [Enable CORS for a Single Route](#enable-cors-for-a-single-route)
  12. * [Configuring CORS](#configuring-cors)
  13. * [Configuring CORS Asynchronously](#configuring-cors-asynchronously)
  14. * [Enabling CORS Pre-Flight](#enabling-cors-pre-flight)
  15. * [Configuration Options](#configuration-options)
  16. * [Demo](#demo)
  17. * [License](#license)
  18. * [Author](#author)
  19. ## Installation
  20. This is a [Node.js](https://nodejs.org/en/) module available through the
  21. [npm registry](https://www.npmjs.com/). Installation is done using the
  22. [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
  23. ```sh
  24. $ npm install cors
  25. ```
  26. ## Usage
  27. ### Simple Usage (Enable *All* CORS Requests)
  28. ```javascript
  29. var express = require('express')
  30. var cors = require('cors')
  31. var app = express()
  32. app.use(cors())
  33. app.get('/products/:id', function (req, res, next) {
  34. res.json({msg: 'This is CORS-enabled for all origins!'})
  35. })
  36. app.listen(80, function () {
  37. console.log('CORS-enabled web server listening on port 80')
  38. })
  39. ```
  40. ### Enable CORS for a Single Route
  41. ```javascript
  42. var express = require('express')
  43. var cors = require('cors')
  44. var app = express()
  45. app.get('/products/:id', cors(), function (req, res, next) {
  46. res.json({msg: 'This is CORS-enabled for a Single Route'})
  47. })
  48. app.listen(80, function () {
  49. console.log('CORS-enabled web server listening on port 80')
  50. })
  51. ```
  52. ### Configuring CORS
  53. ```javascript
  54. var express = require('express')
  55. var cors = require('cors')
  56. var app = express()
  57. var corsOptions = {
  58. origin: 'http://example.com',
  59. optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
  60. }
  61. app.get('/products/:id', cors(corsOptions), function (req, res, next) {
  62. res.json({msg: 'This is CORS-enabled for only example.com.'})
  63. })
  64. app.listen(80, function () {
  65. console.log('CORS-enabled web server listening on port 80')
  66. })
  67. ```
  68. ### Configuring CORS w/ Dynamic Origin
  69. ```javascript
  70. var express = require('express')
  71. var cors = require('cors')
  72. var app = express()
  73. var whitelist = ['http://example1.com', 'http://example2.com']
  74. var corsOptions = {
  75. origin: function (origin, callback) {
  76. if (whitelist.indexOf(origin) !== -1) {
  77. callback(null, true)
  78. } else {
  79. callback(new Error('Not allowed by CORS'))
  80. }
  81. }
  82. }
  83. app.get('/products/:id', cors(corsOptions), function (req, res, next) {
  84. res.json({msg: 'This is CORS-enabled for a whitelisted domain.'})
  85. })
  86. app.listen(80, function () {
  87. console.log('CORS-enabled web server listening on port 80')
  88. })
  89. ```
  90. If you do not want to block REST tools or server-to-server requests,
  91. add a `!origin` check in the origin function like so:
  92. ```javascript
  93. var corsOptions = {
  94. origin: function (origin, callback) {
  95. if (whitelist.indexOf(origin) !== -1 || !origin) {
  96. callback(null, true)
  97. } else {
  98. callback(new Error('Not allowed by CORS'))
  99. }
  100. }
  101. }
  102. ```
  103. ### Enabling CORS Pre-Flight
  104. Certain CORS requests are considered 'complex' and require an initial
  105. `OPTIONS` request (called the "pre-flight request"). An example of a
  106. 'complex' CORS request is one that uses an HTTP verb other than
  107. GET/HEAD/POST (such as DELETE) or that uses custom headers. To enable
  108. pre-flighting, you must add a new OPTIONS handler for the route you want
  109. to support:
  110. ```javascript
  111. var express = require('express')
  112. var cors = require('cors')
  113. var app = express()
  114. app.options('/products/:id', cors()) // enable pre-flight request for DELETE request
  115. app.del('/products/:id', cors(), function (req, res, next) {
  116. res.json({msg: 'This is CORS-enabled for all origins!'})
  117. })
  118. app.listen(80, function () {
  119. console.log('CORS-enabled web server listening on port 80')
  120. })
  121. ```
  122. You can also enable pre-flight across-the-board like so:
  123. ```javascript
  124. app.options('*', cors()) // include before other routes
  125. ```
  126. ### Configuring CORS Asynchronously
  127. ```javascript
  128. var express = require('express')
  129. var cors = require('cors')
  130. var app = express()
  131. var whitelist = ['http://example1.com', 'http://example2.com']
  132. var corsOptionsDelegate = function (req, callback) {
  133. var corsOptions;
  134. if (whitelist.indexOf(req.header('Origin')) !== -1) {
  135. corsOptions = { origin: true } // reflect (enable) the requested origin in the CORS response
  136. } else {
  137. corsOptions = { origin: false } // disable CORS for this request
  138. }
  139. callback(null, corsOptions) // callback expects two parameters: error and options
  140. }
  141. app.get('/products/:id', cors(corsOptionsDelegate), function (req, res, next) {
  142. res.json({msg: 'This is CORS-enabled for a whitelisted domain.'})
  143. })
  144. app.listen(80, function () {
  145. console.log('CORS-enabled web server listening on port 80')
  146. })
  147. ```
  148. ## Configuration Options
  149. * `origin`: Configures the **Access-Control-Allow-Origin** CORS header. Possible values:
  150. - `Boolean` - set `origin` to `true` to reflect the [request origin](http://tools.ietf.org/html/draft-abarth-origin-09), as defined by `req.header('Origin')`, or set it to `false` to disable CORS.
  151. - `String` - set `origin` to a specific origin. For example if you set it to `"http://example.com"` only requests from "http://example.com" will be allowed.
  152. - `RegExp` - set `origin` to a regular expression pattern which will be used to test the request origin. If it's a match, the request origin will be reflected. For example the pattern `/example\.com$/` will reflect any request that is coming from an origin ending with "example.com".
  153. - `Array` - set `origin` to an array of valid origins. Each origin can be a `String` or a `RegExp`. For example `["http://example1.com", /\.example2\.com$/]` will accept any request from "http://example1.com" or from a subdomain of "example2.com".
  154. - `Function` - set `origin` to a function implementing some custom logic. The function takes the request origin as the first parameter and a callback (which expects the signature `err [object], allow [bool]`) as the second.
  155. * `methods`: Configures the **Access-Control-Allow-Methods** CORS header. Expects a comma-delimited string (ex: 'GET,PUT,POST') or an array (ex: `['GET', 'PUT', 'POST']`).
  156. * `allowedHeaders`: Configures the **Access-Control-Allow-Headers** CORS header. Expects a comma-delimited string (ex: 'Content-Type,Authorization') or an array (ex: `['Content-Type', 'Authorization']`). If not specified, defaults to reflecting the headers specified in the request's **Access-Control-Request-Headers** header.
  157. * `exposedHeaders`: Configures the **Access-Control-Expose-Headers** CORS header. Expects a comma-delimited string (ex: 'Content-Range,X-Content-Range') or an array (ex: `['Content-Range', 'X-Content-Range']`). If not specified, no custom headers are exposed.
  158. * `credentials`: Configures the **Access-Control-Allow-Credentials** CORS header. Set to `true` to pass the header, otherwise it is omitted.
  159. * `maxAge`: Configures the **Access-Control-Max-Age** CORS header. Set to an integer to pass the header, otherwise it is omitted.
  160. * `preflightContinue`: Pass the CORS preflight response to the next handler.
  161. * `optionsSuccessStatus`: Provides a status code to use for successful `OPTIONS` requests, since some legacy browsers (IE11, various SmartTVs) choke on `204`.
  162. The default configuration is the equivalent of:
  163. ```json
  164. {
  165. "origin": "*",
  166. "methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
  167. "preflightContinue": false,
  168. "optionsSuccessStatus": 204
  169. }
  170. ```
  171. For details on the effect of each CORS header, read [this](http://www.html5rocks.com/en/tutorials/cors/) article on HTML5 Rocks.
  172. ## Demo
  173. A demo that illustrates CORS working (and not working) using jQuery is available here: [http://node-cors-client.herokuapp.com/](http://node-cors-client.herokuapp.com/)
  174. Code for that demo can be found here:
  175. * Client: [https://github.com/TroyGoode/node-cors-client](https://github.com/TroyGoode/node-cors-client)
  176. * Server: [https://github.com/TroyGoode/node-cors-server](https://github.com/TroyGoode/node-cors-server)
  177. ## License
  178. [MIT License](http://www.opensource.org/licenses/mit-license.php)
  179. ## Author
  180. [Troy Goode](https://github.com/TroyGoode) ([troygoode@gmail.com](mailto:troygoode@gmail.com))
  181. [coveralls-image]: https://img.shields.io/coveralls/expressjs/cors/master.svg
  182. [coveralls-url]: https://coveralls.io/r/expressjs/cors?branch=master
  183. [downloads-image]: https://img.shields.io/npm/dm/cors.svg
  184. [downloads-url]: https://npmjs.org/package/cors
  185. [npm-image]: https://img.shields.io/npm/v/cors.svg
  186. [npm-url]: https://npmjs.org/package/cors
  187. [travis-image]: https://img.shields.io/travis/expressjs/cors/master.svg
  188. [travis-url]: https://travis-ci.org/expressjs/cors