# koa2-response

> A middleware which can let you send your response easier

Latest version **2.0.2** (published 2019-01-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa2-response
pnpm add koa2-response
yarn add koa2-response
bun add koa2-response
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2019-01-22 |
| First published | 2018-07-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | detectiveHLH |
| Maintainers | detectivehlh |
| Keywords | koa, response, middleware |

## Links

- npm: https://www.npmjs.com/package/koa2-response
- Repository: https://github.com/detectiveHLH/koa2-response
- Homepage: https://github.com/detectiveHLH/koa2-response#readme
- Issues: https://github.com/detectiveHLH/koa2-response/issues
- npm.io page: https://npm.io/package/koa2-response

## Dependencies (2)

- [koa](https://npm.io/package/koa.md) ^2.5.2
- [koa-router](https://npm.io/package/koa-router.md) ^7.4.0

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 2.0.2 (latest) — 2019-01-22
- 2.0.1 — 2018-07-27
- 2.0.0 — 2018-07-20
- 1.1.1 — 2018-07-11
- 1.1.0 — 2018-07-11
- 1.0.0 — 2018-07-10

## README

<h1 align="center">koa2-response</h1>
A middleware which can let you send your response easier in Koa.

Install
=
```bash
npm install koa2-response
```

Usage
=
```javascript
const koa = require('koa');
const app = new koa();
const router = require('koa-router')();
const response = require('koa2-response');

// Defining error code map
const code = {
  UNKNOWN_ERROR: [1, 'Sorry, you seem to have encountered some unknown errors.']
};

// Use this middleware
app.use(response);

router
  .get('/', (ctx, next) => {
    // Instead of "ctx.body = {};"
    ctx.success({
      name: 'test'
    })
  })
  .get('/error_test', (ctx, next) => {
     ctx.error(code.UNKNOWN_ERROR);
  })
  .get('/throw', (ctx, next) => {
    ctx.throw(500, 'Internal Server Error');
  });

app.use(router.routes());
app.use(router.allowedMethods());

app.listen(3000);

console.log(`Server is running on port 3000`);
```
     
Licences
=
[MIT](https://github.com/detectiveHLH/koa2-response/blob/master/LICENSE)

---
_Source: https://npm.io/package/koa2-response · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
