2.0.2 • Published 5 years ago

koa2-response v2.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

Install

npm install koa2-response

Usage

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

2.0.2

5 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago