Skip to content

Middleware for Koa that adds some common methods to the Koa response.

License

Notifications You must be signed in to change notification settings

jovey-zheng/koa-reply

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

koa-reply

Middleware for Koa that adds some common methods to the Koa response.

npm version npm download

Install

$ npm i koa-reply -S

Description

There are method and corresponding status code:

  • OK: 200
  • Created: 201
  • BadRequest: 400
  • UnAuthorized: 401
  • Forbidden: 403
  • NotFound: 404
  • InternalServerError: 500

Or you can add your optiosns:

const koa = require('koa');
const reply = require('koa-reply');

const app = koa();

app.use(reply({
  myMethod: code
  //...
}));
//...

Usage

Check the example

const koa = require('koa');
const router = require('koa-router')();
const reply = require('koa-reply');

const app = koa();
// add reply middleware on top level
app.use(reply());

// test router
router.get('/', function* () {
  this.response.OK = 'hello world';
});
router.get('/404', function* () {
  this.response.NotFound = 'Not Found'
});
router.post('/create', function* () {
  this.response.Created = 'success';
});
// ... others

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

app.listen(process.env.PORT || 3000, function() {
  console.log('Using `replt` middleware in test');
});

About

Middleware for Koa that adds some common methods to the Koa response.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages