Skip to content

Commit 1fb8bd3

Browse files
authored
feat: addition of the CORS section (#145)
* feat: addition of CORS section
1 parent de4f270 commit 1fb8bd3

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ with only a subset of sections having recommendations):
7676
- [Code Coverage](./docs/development/code-coverage.md)
7777
- [TypeScript](./docs/development/typescript.md)
7878
- [Accessibility](./docs/development/accessibility.md)
79-
- Cross origin resource sharing (CORS)
79+
- [Cross Origin Communication](./docs/development/cross-origin.md)
8080
- [CI/CD](./docs/development/ci-cd.md)
8181
- Npm
8282
- [Npm Proxy / Internal Registries](./docs/development/npm-proxy.md)

docs/assets/cors-error.png

94.5 KB
Loading

docs/development/cross-origin.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Cross Origin Communication
2+
3+
4+
## Guidance
5+
6+
There are two patterns that the team is familiar with when dealing with Cross Origin Communication. Those are Cross Origin Resource Sharing(CORS) and using a Proxy server.
7+
8+
## Cross Origin Resource Sharing (CORS)
9+
10+
By default, browers assume that your backend server doesn't want to share its resources with a front-end that it doesn't know about.
11+
12+
You might have seen this error in the browsers dev console:
13+
14+
![cors error from chrome dev tools](../assets/cors-error.png)
15+
16+
We have a [small and simple application](https://github.com/nodeshift-blog-examples/cors-ref-arch-demo) that demonstrates getting a CORS error and how to fix it.
17+
18+
However, there are certain situations where your backend might want to be accessible to a front-end application, like a website, CORS should be enabled. Listed below are some situations, where you might want to make sure CORS is enabled
19+
20+
### External APIs
21+
22+
When developing a backend with a public API and you would like to control access to certain resources and how they are used, CORS should be enabled.
23+
24+
**note: When enabling CORS, it is important to limit those methods and headers your application allows to prevent unwanted actors from accessesing resources they shouldn't be accessing.**
25+
26+
27+
### Access to Multiple Environments
28+
29+
As part of the development and testing process of a front-end application, you might want to test against multiple environments, like staging and pre-productions, before pushing to production. If CORS is not enabled on each of these backends, the front-end application will fail to communicate with them.
30+
31+
The team recommends using environment variables to control allowed origin hosts. This allows keeping CORS enabled locally, but provides different values for each environment.
32+
33+
## Proxy Server
34+
35+
The team also has experience doing cross origin communication using a Proxy Server.
36+
37+
This is helpful when you can't make requests directly from your front-end application to the API server. Your front-end application might only be able to make requests to its host server. The host server would then proxy those requests to the target API server.
38+
39+
## Recommended Components
40+
41+
[cors](https://www.npmjs.com/package/cors) is a node.js package for providing a Connect/Express middleware that can be used to enable CORS with various options.
42+
43+
Most web frameworks, like express.js, have [similar modules](https://www.npmjs.com/search?q=cors) to help easily enable and manage CORS requests
44+
45+
## Further Reading
46+
47+
* [cors](https://www.npmjs.com/package/cors)
48+
49+
* [CORS module search on npm](https://www.npmjs.com/search?q=cors)
50+
51+
* [CORS on mdn](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)

npcheck.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,16 @@
428428
"section": [
429429
"Consuming Services"
430430
]
431+
},
432+
{
433+
"name": "cors",
434+
"npmlink": "https://www.npmjs.com/package/cors",
435+
"reviewlevel": "Yearly Watch",
436+
"activitycurrent": "None",
437+
"activitytarget": "None",
438+
"section": [
439+
"CORS"
440+
]
431441
}
432442
],
433443
"licenses": {

0 commit comments

Comments
 (0)