https://github.com/jovey-zheng/koa-reply
Middleware for Koa that adds some common methods to the Koa response.
https://github.com/jovey-zheng/koa-reply
koa middleware response
Last synced: 3 months ago
JSON representation
Middleware for Koa that adds some common methods to the Koa response.
- Host: GitHub
- URL: https://github.com/jovey-zheng/koa-reply
- Owner: jovey-zheng
- License: mit
- Created: 2017-03-10T06:13:51.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-03-14T06:53:09.000Z (about 8 years ago)
- Last Synced: 2024-04-24T19:24:02.682Z (about 1 year ago)
- Topics: koa, middleware, response
- Language: JavaScript
- Size: 7.81 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# koa-reply
Middleware for Koa that adds some common methods to the Koa response.
[](https://www.npmjs.com/package/koa-reply)
[](https://www.npmjs.com/package/koa-reply)# Install
```sh
$ 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**: 500Or you can add your optiosns:
```js
const koa = require('koa');
const reply = require('koa-reply');const app = koa();
app.use(reply({
myMethod: code
//...
}));
//...
```# Usage
Check the [example](./example.js)
```js
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';
});
// ... othersapp.use(router.routes());
app.use(router.allowedMethods());app.listen(process.env.PORT || 3000, function() {
console.log('Using `replt` middleware in test');
});
```