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: _includes/readmes/cors.md
+39-17Lines changed: 39 additions & 17 deletions
Display the source diff
Display the rich diff
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
*[License](#license)
22
22
*[Author](#author)
@@ -69,6 +69,8 @@ app.listen(80, function () {
69
69
70
70
### Configuring CORS
71
71
72
+
See the [configuration options](#configuration-options) for details.
73
+
72
74
```javascript
73
75
var express =require('express')
74
76
var cors =require('cors')
@@ -161,27 +163,45 @@ NOTE: When using this middleware as an application level middleware (for
161
163
example, `app.use(cors())`), pre-flight requests are already handled for all
162
164
routes.
163
165
164
-
### Configuring CORS Asynchronously
166
+
### Customizing CORS Settings Dynamically per Request
165
167
166
-
```javascript
167
-
var express =require('express')
168
-
var cors =require('cors')
169
-
var app =express()
168
+
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.
169
+
170
+
The function accepts:
171
+
1.**`req`**:
172
+
- The incoming request object.
173
+
174
+
2.**`callback(error, corsOptions)`**:
175
+
- A function used to return the computed CORS options.
176
+
-**Arguments**:
177
+
-**`error`**: Pass `null` if there’s no error, or an error object to indicate a failure.
178
+
-**`corsOptions`**: An object specifying the CORS policy for the current request.
170
179
171
-
var allowlist = ['http://example1.com', 'http://example2.com']
172
-
varcorsOptionsDelegate=function (req, callback) {
180
+
Here’s an example that handles both public routes and restricted, credential-sensitive routes:
181
+
182
+
```javascript
183
+
vardynamicCorsOptions=function(req, callback) {
173
184
var corsOptions;
174
-
if (allowlist.indexOf(req.header('Origin')) !==-1) {
175
-
corsOptions = { origin:true } // reflect (enable) the requested origin in the CORS response
185
+
if (req.path.startsWith('/auth/connect/')) {
186
+
corsOptions = {
187
+
origin:'http://mydomain.com', // Allow only a specific origin
188
+
credentials:true, // Enable cookies and credentials
189
+
};
176
190
} else {
177
-
corsOptions = { origin:false } //disable CORS for this request
191
+
corsOptions = { origin:'*' }; //Allow all origins for other routes
178
192
}
179
-
callback(null, corsOptions)// callback expects two parameters: error and options
180
-
}
193
+
callback(null, corsOptions);
194
+
};
181
195
182
-
app.get('/products/:id', cors(corsOptionsDelegate), function (req, res, next) {
183
-
res.json({msg:'This is CORS-enabled for an allowed domain.'})
184
-
})
196
+
app.use(cors(dynamicCorsOptions));
197
+
198
+
app.get('/auth/connect/twitter', function (req, res) {
199
+
res.send('CORS dynamically applied for Twitter authentication.');
200
+
});
201
+
202
+
app.get('/public', function (req, res) {
203
+
res.send('Public data with open CORS.');
204
+
});
185
205
186
206
app.listen(80, function () {
187
207
console.log('CORS-enabled web server listening on port 80')
@@ -192,7 +212,9 @@ app.listen(80, function () {
192
212
193
213
*`origin`: Configures the **Access-Control-Allow-Origin** CORS header. Possible values:
194
214
-`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.
195
-
-`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.
215
+
-`String` - set `origin` to a specific origin. For example, if you set it to
216
+
-`"http://example.com"` only requests from "http://example.com" will be allowed.
217
+
-`"*"` for all domains to be allowed.
196
218
-`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".
197
219
-`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".
198
220
-`Function` - set `origin` to a function implementing some custom logic. The function takes the request origin as the first parameter and a callback (called as `callback(err, origin)`, where `origin` is a non-function value of the `origin` option) as the second.
### The Official Documentation of the Express JS Framework
341
341
342
-
This is the contribution documentation for the [Expressjs.com](https://github.com/expressjs/expressjs.com) website.
342
+
This is the contribution documentation for the [expressjs.com](https://github.com/expressjs/expressjs.com) website.
343
343
344
344
345
345
346
346
#### Need some ideas? These are some typical issues.
347
347
348
-
1.**Website issues**:
349
-
If you see anything on the site that could use a tune-up, think about how to fix it.
350
-
348
+
1.**Website issues**: If you see anything on the site that could use a tune-up, think about how to fix it.
351
349
- Display or screen sizing problems
352
350
- Mobile responsiveness issues
353
351
- Missing or broken accessibility features
354
352
- Website outages
355
353
- Broken links
356
354
- Page structure or user interface enhancements
357
355
358
-
359
-
2.**Content Issues**:
360
-
Fix anything related to site content or typos.
356
+
2.**Content Issues**: Fix anything related to site content or typos.
361
357
- Spelling errors
362
358
- Incorrect/outdated Express JS documentation
363
359
- Missing content
364
360
365
-
366
361
3.**Translation Issues**: Fix any translation errors or contribute new content.
367
362
- Fix spelling errors
368
363
- Fix incorrect/poorly translated words
369
-
- Translate new content
370
-
> **IMPORTANT:**
371
-
> All translation submissions are currently paused. See this [notice](#notice-we-have-paused-all-translation-contributions) for more information.
372
-
373
-
- Check out the [Contributing translations](#contributing-translations) section below for a contributing guide.
364
+
- Check out the [Contributing translations](#contributing-translations) section below for a contributing guide.
374
365
375
366
#### Want to work on a backlog issue?
376
367
@@ -380,6 +371,7 @@ We often have bugs or enhancements that need work. You can find these under our
380
371
381
372
If you've found a bug or a typo, or if you have an idea for an enhancement, you can:
382
373
- Submit a [new issue](https://github.com/expressjs/expressjs.com/issues/new/choose) on our repo. Do this for larger proposals, or if you'd like to discuss or get feedback first.
374
+
383
375
- Make a [Github pull request](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request). If you have already done work and it's ready to go, feel free to send it our way.
384
376
385
377
## Getting Started
@@ -400,7 +392,9 @@ So you've found a problem that you want to fix, or have a site enhancement you w
After you've got the code you're ready to start making your changes!
406
400
@@ -426,64 +420,41 @@ follow the specific instructions for [How to write a blog post.](https://express
426
420
**CSS or Javascript**
427
421
- All css and js files are kept in `css` and `js` folders on the project root.
428
422
429
-
The Express JS website is build using [Jeykyll](https://jekyllrb.com/) and is hosted on [Github Pages](https://pages.github.com/).
423
+
The Express JS website is built using [Jekyll](https://jekyllrb.com/) and is hosted on [Github Pages](https://pages.github.com/).
430
424
431
425
#### Step 3: Running the Application
432
426
433
-
434
427
Now you'll need a way to see your changes, which means you'll need a running version of the application. You have two options.
435
-
1.__Run Locally__: This gets the local version of the application up and running on your machine. Follow our [Local Setup Guide](https://github.com/expressjs/expressjs.com?tab=readme-ov-file#local-setup) to use this option.
428
+
429
+
430
+
1.__Run Locally__: This gets the local version of the application up and running on your machine. Follow our [Local Setup Guide](https://github.com/expressjs/expressjs.com?tab=readme-ov-file#build-the-website-locally) to use this option.
436
431
- This is the recommended option for moderate to complex work.
432
+
437
433
2.__Run using Deploy Preview__: Use this option if you don't want to bother with a local installation. Part of our continuous integration pipeline includes [Netlify Deploy Preview](https://docs.netlify.com/site-deploys/deploy-previews/).
438
434
1. To use this you'll need to get your changes online - after you've made your first commit on your feature branch, make a *draft* pull request.
439
435
2. After the build steps are complete, you'll have access to a __Deploy Preview__ tab that will run your changes on the web, rebuilding after each commit is pushed.
440
436
3. After you are completely done your work and it's ready for review, remove the draft status on your pull request and submit your work.
441
437
442
438
## Contributing translations
443
439
444
-
#### Notice: We have paused all translation contributions.
445
-
> **IMPORTANT:**
446
-
> We are currently working toward a more streamlined translations workflow. As long as this notice is posted, we will _not_ be accepting any translation submissions.
447
-
448
-
We highly encourage community translations! We no longer have professional translations, and we believe in the power of our community to provide accurate and helpful translations.
440
+
We use Crowdin to manage our translations in multiple languages and achieve automatic translation with artificial intelligence. Since these translations can be inefficient in some cases, we need help from the community to provide accurate and helpful translations.
449
441
450
442
The documentation is translated into these languages:
443
+
444
+
- Chinese Simplified (`zh-cn`)
445
+
- Chinese Traditional (`zh-tw`)
451
446
- English (`en`)
452
-
- Spanish (`es`)
453
447
- French (`fr`)
448
+
- German (`de`)
454
449
- Italian (`it`)
455
-
- Indonesian (`id`)
456
450
- Japanese (`ja`)
457
451
- Korean (`ko`)
458
452
- Brazilian Portuguese (`pt-br`)
459
-
- Russian (`ru`)
460
-
- Slovak (`sk`)
461
-
- Thai (`th`)
462
-
- Turkish (`tr`)
463
-
- Ukrainian (`uk`)
464
-
- Uzbek (`uz`)
465
-
- Simplified Chinese (`zh-cn`)
466
-
- Traditional Chinese (`zh-tw`)
467
-
468
-
### Adding New Full Site Translations
469
-
470
-
If you find a translation is missing from the list you can create a new one.
471
-
472
-
To translate Expressjs.com into a new language, follow these steps:
473
-
474
-
1. Clone the [`expressjs.com`](https://github.com/expressjs/expressjs.com) repository.
475
-
2. Create a directory for the language of your choice using its [ISO 639-1 code](https://www.loc.gov/standards/iso639-2/php/code_list.php) as its name.
476
-
3. Copy `index.md`, `api.md`, `starter/`, `guide/`, `advanced/`, `resources/`, `4x/`, and `3x/`, to the language directory.
477
-
4. Remove the link to 2.x docs from the "API Reference" menu.
478
-
5. Update the `lang` variable in the copied markdown files.
479
-
6. Update the `title` variable in the copied markdown files.
480
-
7. Create the header, footer, notice, and announcement file for the language in the `_includes/` directory, in the respective directories, and make necessary edits to the contents.
481
-
8. Create the announcement file for the language in the `_includes/` directory.
482
-
9. Make sure to append `/{{ page.lang }}` to all the links within the site.
483
-
10. Update the [CONTRIBUTING.md](https://github.com/expressjs/expressjs.com/blob/gh-pages/CONTRIBUTING.md#contributing-translations) and the `.github/workflows/translation.yml` files with the new language.
453
+
- Spanish (`es`)
484
454
485
-
### Adding Page and Section Translations
455
+
### How to translate
486
456
487
-
Many site translations are still missing pages. To find which ones we need help with, you can [filter for merged PRs](https://github.com/expressjs/expressjs.com/pulls?q=is%3Apr+is%3Aclosed+label%3Arequires-translation-es) that include the tag for your language, such as `requires-translation-es` for requires Spanish translation.
457
+
1. Request to join the Express.js Website project on [Crowdin](https://express.crowdin.com/website)
458
+
2.[Select the language you want to translate](https://support.crowdin.com/joining-translation-project/#starting-translation)
If you contribute a page or section translation, please reference the original PR. This helps the person merging your translation to remove the tag from the original PR.
0 commit comments