|
| 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 | + |
| 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) |
0 commit comments