forked from balderdashy/sails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core documentation and cleanup in CORS hook.
- Loading branch information
1 parent
ab81886
commit e0b54da
Showing
7 changed files
with
238 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# cors (Core Hook) | ||
|
||
|
||
## Status | ||
|
||
> ##### Stability: [2](https://github.com/balderdashy/sails-docs/blob/master/contributing/stability-index.md) - Stable | ||
|
||
|
||
## Dependencies | ||
|
||
In order for this hook to load, the following other hooks must have already finished loading: | ||
|
||
- moduleloader | ||
- userconfig | ||
|
||
|
||
## Dependents | ||
|
||
If this hook is disabled, in order for Sails to load, the following other core hooks must also be disabled: | ||
|
||
_N/A_ | ||
|
||
|
||
## Purpose | ||
|
||
This hook's responsibilities are: | ||
|
||
|
||
##### Bind shadow routes to set appropriate CORS headers | ||
|
||
When Sails loads, this hook binds a `router:before` listener so that it can bind routes before the router binds explicit routes. Then it binds shadow routes for the appropriate endpoints based on `sails.config.cors` (also mixing in its implicit defaults). | ||
|
||
|
||
|
||
## Implicit Defaults | ||
|
||
This hook sets the following implicit default configuration on `sails.config`: | ||
|
||
|
||
| Property | Type | Default | | ||
|-----------------------------------------------|:-------------:|-----------------| | ||
| `sails.config.cors.origin` | ((string)) | `'*'` | ||
| `sails.config.cors.credentials` | ((boolean)) | `true` | ||
| `sails.config.cors.methods` | ((string)) | `'GET, POST, PUT, DELETE, OPTIONS, HEAD'` | ||
| `sails.config.cors.headers` | ((string)) | `'content-type'` | ||
| `sails.config.cors.exposeHeaders` | ((string)) | `''` _(empty string)_ | ||
| `sails.config.cors.securityLevel` | ((number)) | `0` | ||
|
||
|
||
|
||
|
||
## Events | ||
|
||
##### `hook:cors:loaded` | ||
|
||
Emitted when this hook has been automatically loaded by Sails core, and triggered the callback in its `initialize` function. | ||
|
||
|
||
|
||
|
||
## FAQ | ||
|
||
> If you have a question that isn't covered here, please feel free to send a PR adding it to this section (even if you don't have the answer!) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module.exports = function (req, res, next) { | ||
|
||
// If we can set headers, do so. | ||
// (Note: This is for backwards-compatibility. `res.set()` should always exist now. | ||
// This check can be removed in a future version of Sails- just needs tests first.) | ||
if (res.set) { | ||
res.set('Access-Control-Allow-Origin', ''); | ||
res.set('Access-Control-Allow-Credentials', ''); | ||
res.set('Access-Control-Allow-Methods', ''); | ||
res.set('Access-Control-Allow-Headers', ''); | ||
res.set('Access-Control-Expose-Headers', ''); | ||
} | ||
|
||
next(); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.