Skip to content

Latest commit

 

History

History
38 lines (30 loc) · 959 Bytes

File metadata and controls

38 lines (30 loc) · 959 Bytes

koa2-response

A middleware which can let you send your response easier

Install

npm install koa2-response

Usage

    const koa = require('koa');
    const app = new koa();
    const router = require('koa-router')();
    const response = require('koa2-response');
    
    const code = {
      UNKNOWN_ERROR: [1, 'Sorry, you seem to have encountered some unknown errors.']
    }
    
    router
      .get('/', (ctx, next) => {
        response.success(ctx, {
          name: 'test'
        })
      })
      .get('/error_test', (ctx, next) => {
         response.error(ctx, code.UNKNOWN_ERROR);
      })
    
    app.use(router.routes());
    app.use(router.allowedMethods());
    
    app.listen(3000);
    
    console.log(`Server is running on port 3000`);

Licences

MIT