egg-response
English|中文
Dependency Description
Dependent egg version
Egg-response version | egg 1.x |
---|---|
1.x | 😁 |
0.x | ❌ |
Installation
$ npm i egg-response --saveor$ yarn add egg-response
Open the plugin
// config/plugin.jsexportsresponse = enable: true package: 'egg-response';
Where to use
// {app_root}/app/controller/post.js ctx; // or ctx;
API
ctx.successful(data[,options])
Create a successful response
Parameter
- data - the data returned by the response
- options (optional) -
Object
with the following optional attributes:- isData -
boolean
, whether to return data, the default value istrue
,If set to false, the returned json fielddata
will be replaced bymessage
- code -
int
, response status code, default value is200
- extra -
Object
, additional information
- isData -
Example
ctx;
- Returns the result, will be in json format, as follows:
ctx.failed([options])
Create a failed response
Parameter
- options (optional) -
Object
with the following optional attributes:- message - error message, default value is 'Request error'
- code - response status code, default value is
500
- extra - append message
Example
ctx;
- Returns the result,will be in json format, as follows:
Outdated (v1.1.0)
ctx.createSuccessResponse(data[,code])
with
ctx.createFailedResponse([message[,code]])
Detailed configuration
Go to config/config.default.js for detailed configuration item descriptions.
Asking questions
Please go to egg issues for asynchronous communication.