Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including egg-response with all npm packages installed. Try it out:

egg-response lists no main file and has no index.js, so it can't be directly required. If this is a mistake, please let us know. It may however contain internal files that you can require manually:

// require("egg-response/[??]")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

egg-response v1.8.1

response plugin for egg

egg-response

English|中文

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Dependency Description

Dependent egg version

Egg-response versionegg 1.x
1.x😁
0.x

Installation

$ npm i egg-response --save
or
$ yarn add egg-response

Open the plugin

// config/plugin.js
exports.response = {
  enable: true,
  package: 'egg-response',
};

Where to use

// {app_root}/app/controller/post.js

ctx.successful(data[,options]);

// or

ctx.failed([options]);

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 is true,If set to false, the returned json field data will be replaced by message
    • code -int, response status code, default value is 200
    • extra -Object, additional information

Example

ctx.successful([1,2,3],{
  extra: {
    xxx: 'Additional message'
  }
});
  • Returns the result, will be in json format, as follows:
{
  "success": true,
  "code": 200,
  "data": [1,2,3],
  "xxx": "Additional message"
}

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.failed({ message: 'There is no such category' });
  • Returns the result,will be in json format, as follows:
{
  "code": 500,
  "success": false,
  "message": "There is no such category"
}

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] (https://github.com/iamljw/egg-response/issues) for asynchronous communication.

License

MIT

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free