1.0.3 • Published 8 years ago

koa-json-replacer v1.0.3

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

koa-json-replacer

Replacer JSON response middleware.

Installation

$ npm install koa-json-replacer

Test

$ npm test

Example

koa-v1.x

var koa = require('koa');
var app = koa();
var koaJsonReplacer = require('koa-json-replacer');

app.use(koaJsonReplacer(function (key, value) {
	if (this[key] instanceof String) {
		value = this[key] + '123';
	}
	return value;
}));

app.use(function *(next){
  this.body = { foo: 'bar' };
});

koa-v2.x

var koa = require('koa');
var app = koa();
var koaJsonReplacer = require('koa-json-replacer');
var convert = require('koa-convert');

app.use(convert(koaJsonReplacer(function (key, value) {
	if (this[key] instanceof String) {
		value = this[key] + '123';
	}
	return value;
})));

app.use((ctx, next) => {
  ctx.body = { foo: 'bar' };
});

yields:

$ GET /

{
  "foo": "bar123"
}

License

MIT

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago