Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Custom Responses.md, added some clarifications #7258

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -56,11 +56,53 @@ This approach has many advantages:
- Content negotiation (JSON vs HTML) is taken care of
- API tweaks can be done in one quick edit to the appropriate generic response file


### Response methods and files

Any `.js` file saved in the `api/responses/` folder can be executed by calling `res.thatFileName()`. For example, `api/responses/insufficientFunds.js` can be executed with a call to `res.insufficientFunds()`.


### Using custom responses in actions2 controllers

If you are using the new, recommended modern standard for controllers "actions2", using custom responses is as easy as defining an input:

```javascript
module.exports = {
friendlyName: 'Create Account',

description: 'Create a new account',

inputs: {
page: {...},

limit: {...}
},

exits: {
newAccount: {
description: "A custom response, which is defined in api/responses as `newAccountCreated.js`",
responseType: 'newAccountCreated'
},
badRequest: {
// this does not need to be defined, Sails will handle `exits.badRequest()` automatically
description: "Points to either `api/responses/badRequest.js` or the built-in version",
responseType: 'badRequest'
},
serverError: {
// this does not need to be defined, Sails will handle `exits.serverError()` automatically
description: "Points to either `api/responses/serverError.js` or the built-in version",
responseType: 'serverError'
}
},

fn: (inputs, exits) => {...}
};
```

Then, you can use `exits.newAccount()` to trigger the custom response.

See: [actions2](https://sailsjs.com/documentation/concepts/actions-and-controllers#actions2) for more details.


##### Accessing `req`, `res`, and `sails`

The request and response objects are available inside of a custom response as `this.req` and `this.res`. This allows the actual response function to take arbitrary parameters. For example:
Expand Down Expand Up @@ -95,6 +137,13 @@ module.exports = function insufficientFunds(err, extraInfo){

All Sails apps have several pre-configured responses like [`res.serverError()`](https://sailsjs.com/documentation/reference/response-res/res-server-error) and [`res.notFound()`](https://sailsjs.com/documentation/reference/response-res/res-not-found) that can be used even if they don’t have corresponding files in `api/responses/`.

The built-in responses are:
- ok (200)
- badRequest (400)
- forbidden (403)
- notFound (404)
- serverError (500)

Any of the default responses may be overridden by adding a file with the same name to `api/responses/` in your app (e.g. `api/responses/serverError.js`).

> You can use the [Sails command-line tool](https://sailsjs.com/documentation/reference/command-line-interface/sails-generate) as a shortcut for doing this.
Expand Down