You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+36-16
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,7 @@ CORS is a node.js package for providing a [Connect](http://www.senchalabs.org/co
16
16
*[Configuring CORS](#configuring-cors)
17
17
*[Configuring CORS w/ Dynamic Origin](#configuring-cors-w-dynamic-origin)
18
18
*[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)
20
20
*[Configuration Options](#configuration-options)
21
21
*[Demo](#demo)
22
22
*[License](#license)
@@ -70,6 +70,8 @@ app.listen(80, function () {
70
70
71
71
### Configuring CORS
72
72
73
+
See the [configuration options](#configuration-options) for details.
74
+
73
75
```javascript
74
76
var express =require('express')
75
77
var cors =require('cors')
@@ -162,27 +164,45 @@ NOTE: When using this middleware as an application level middleware (for
162
164
example, `app.use(cors())`), pre-flight requests are already handled for all
163
165
routes.
164
166
165
-
### Configuring CORS Asynchronously
167
+
### Customizing CORS Settings Dynamically per Request
166
168
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.
171
180
172
-
var allowlist = ['http://example1.com', 'http://example2.com']
173
-
varcorsOptionsDelegate=function (req, callback) {
181
+
Here’s an example that handles both public routes and restricted, credential-sensitive routes:
182
+
183
+
```javascript
184
+
vardynamicCorsOptions=function(req, callback) {
174
185
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
+
};
177
191
} else {
178
-
corsOptions = { origin:false } //disable CORS for this request
192
+
corsOptions = { origin:'*' }; //Allow all origins for other routes
179
193
}
180
-
callback(null, corsOptions)// callback expects two parameters: error and options
181
-
}
194
+
callback(null, corsOptions);
195
+
};
182
196
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
+
});
186
206
187
207
app.listen(80, function () {
188
208
console.log('CORS-enabled web server listening on port 80')
0 commit comments