koa-custom-statuses

1.5.2 • Public • Published

Koa Custom Statuses

NPM version build status Test coverage David deps iojs version node version npm download

Koa has a defined set of HTTP response codes that can be retured in a request. While they are shandard HTTP codes and will be enough most of the time, Sometimes, there is a need for creating custom codes. This module enables you to achieve just that declaring the additional codes you require in the very beginning. It also enables you to change the status message of existing status codes.

The module definition is extremely simple. Just wrap a koa function into the module along with an object containing the status code you require.

 
var koa = require('koa')
var app = koa()
require('koa-custom-statuses')(app, {
    '200': 'Im Loving it!!',
    '499': 'Cos I can come that close to 500',
    '700': 'Theres the 7 series in HTTP',
    '999': 'Because We can'
});
 
app.use(funcion(){
    //This will no longer throw an invalid code error
    this.status = 700;
})
 

There are just some basics you need to keep in mind.

  • The status codes can be 3 digit numbers only.
  • The codes you declare will overwrite the Koa Defaults.
  • You can only use default Koa codes or the codes defined in the module. Any others will throw an error

License

MIT

Package Sidebar

Install

npm i koa-custom-statuses

Weekly Downloads

7

Version

1.5.2

License

MIT

Last publish

Collaborators

  • srinivasiyer