Skip to content

Commit 7a61f85

Browse files
committed
update the docs for per request config
1 parent 53312a5 commit 7a61f85

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

README.md

+36-16
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ CORS is a node.js package for providing a [Connect](http://www.senchalabs.org/co
1616
* [Configuring CORS](#configuring-cors)
1717
* [Configuring CORS w/ Dynamic Origin](#configuring-cors-w-dynamic-origin)
1818
* [Enabling CORS Pre-Flight](#enabling-cors-pre-flight)
19-
* [Configuring CORS Asynchronously](#configuring-cors-asynchronously)
19+
* [Customizing CORS Settings Dynamically per Request](#customizing-cors-settings-dynamically-per-request)
2020
* [Configuration Options](#configuration-options)
2121
* [Demo](#demo)
2222
* [License](#license)
@@ -70,6 +70,8 @@ app.listen(80, function () {
7070

7171
### Configuring CORS
7272

73+
See the [configuration options](#configuration-options) for details.
74+
7375
```javascript
7476
var express = require('express')
7577
var cors = require('cors')
@@ -162,27 +164,45 @@ NOTE: When using this middleware as an application level middleware (for
162164
example, `app.use(cors())`), pre-flight requests are already handled for all
163165
routes.
164166

165-
### Configuring CORS Asynchronously
167+
### Customizing CORS Settings Dynamically per Request
166168

167-
```javascript
168-
var express = require('express')
169-
var cors = require('cors')
170-
var app = express()
169+
For APIs that require different CORS configurations for specific routes or requests, you can dynamically generate CORS options based on the incoming request. The `cors` middleware allows you to achieve this by passing a function instead of static options. This function is called for each incoming request and must use the callback pattern to return the appropriate CORS options.
170+
171+
The function accepts:
172+
1. **`req`**:
173+
- The incoming request object.
174+
175+
2. **`callback(error, corsOptions)`**:
176+
- A function used to return the computed CORS options.
177+
- **Arguments**:
178+
- **`error`**: Pass `null` if there’s no error, or an error object to indicate a failure.
179+
- **`corsOptions`**: An object specifying the CORS policy for the current request.
171180

172-
var allowlist = ['http://example1.com', 'http://example2.com']
173-
var corsOptionsDelegate = function (req, callback) {
181+
Here’s an example that handles both public routes and restricted, credential-sensitive routes:
182+
183+
```javascript
184+
var dynamicCorsOptions = function(req, callback) {
174185
var corsOptions;
175-
if (allowlist.indexOf(req.header('Origin')) !== -1) {
176-
corsOptions = { origin: true } // reflect (enable) the requested origin in the CORS response
186+
if (req.path.startsWith('/auth/connect/')) {
187+
corsOptions = {
188+
origin: 'http://mydomain.com', // Allow only a specific origin
189+
credentials: true, // Enable cookies and credentials
190+
};
177191
} else {
178-
corsOptions = { origin: false } // disable CORS for this request
192+
corsOptions = { origin: '*' }; // Allow all origins for other routes
179193
}
180-
callback(null, corsOptions) // callback expects two parameters: error and options
181-
}
194+
callback(null, corsOptions);
195+
};
182196

183-
app.get('/products/:id', cors(corsOptionsDelegate), function (req, res, next) {
184-
res.json({msg: 'This is CORS-enabled for an allowed domain.'})
185-
})
197+
app.use(cors(dynamicCorsOptions));
198+
199+
app.get('/auth/connect/twitter', function (req, res) {
200+
res.send('CORS dynamically applied for Twitter authentication.');
201+
});
202+
203+
app.get('/public', function (req, res) {
204+
res.send('Public data with open CORS.');
205+
});
186206

187207
app.listen(80, function () {
188208
console.log('CORS-enabled web server listening on port 80')

0 commit comments

Comments
 (0)