koajs / json Goto Github PK
View Code? Open in Web Editor NEWpretty-printed JSON response middleware
License: MIT License
pretty-printed JSON response middleware
License: MIT License
Is it possible to customize the serializer?
My applications use BigInt and V8 (just as Chrome) throws a nice:
TypeError: Do not know how to serialize a BigInt
at JSON.stringify (<anonymous>)
at next.then (/Users/damz/Desktop/cfciampino-reloaded/node_modules/koa-json/index.js:50:32)
I'd like to apply a workaround like (see next) when returning my payloads:
tc39/proposal-bigint#146 (comment)
Good jobs guys!
kinda redundant to have both, unless we support header fields or something else... but meh
RT, if the callback param exists, change this to the jsonp?
It would be nice if there was an option for the prettify to happen automatically based on an accept header, something like this:
switch (ctx.accepts('json', 'html', 'text')) {
json: break;
html:
text:
// pretty print json here
default: break;
}
The following results in a payload hola
and not the json "hola"
.
import http from 'http';
import koa from 'koa';
import json from 'koa-json';
const app = new koa();
app.use(json());
app.use(ctx => ctx.body = 'hola');
http.createServer(app.callback()).listen(1234);
Current workaround:
app.use(async (ctx, next) => {
await next();
if (typeof ctx.body === 'string')
ctx.body = `"${ctx.body}"`;
});
this is my code
const middlewares = compose([
favicon(__dirname + '/favicon.ico'),
bodyParser({formLimit: '5mb'}),
json(),
router.routes(),
router.allowedMethods()
])
app.use(middlewares)
Hello,
koa is a middleware in my scene, I use koa to send HTTP request and then reponse to browser.
here is my code:
var reqHttp = http.request(opt, function(resHttp){
if (resHttp.statusCode == 200){
var buf = "";
resHttp.on('data', function(data) {
buf += data;
});
resHttp.on('end', function(){
ctx.body = JSON.parse(buf); // throw error: Can't remove headers after they are sent.
});
} else {
ctx.body = {error: -1};
}
});
reqHttp.write(data);
reqHttp.end();
I just want a like res.json
in express for koa...
please help me ?
Module doesn't seem to be working at all for me.
...
app.use(require('koa-json')());
router.get('/test', function *(next) {
this.body = {test: 'testing'};
});
...
... go to /test, doesn't look pretty.
Digging into the code I see that it doesn't print pretty if this.body
is a string, but by the time koa-json code is run JSON seems to have been stringified already. Is my usage wrong or has koa changed how/when it stringifies JSON which means koa-json no longer works?
give me npm access. i'll fix
If route returns null or undefined or an object with a null or undefined property then body/response is empty!
app.use(async (ctx, next) => {
try {
await next();
ctx.body = null; //empty response
ctx.body = undefined; //empty response
ctx.body = { data: null }; //empty response
ctx.body = { data: undefined }; //empty response
}
catch(error) {
ctx.status = 400;
ctx.body = error.message || error;
}
});
I think that the defaults should be the same for both of these. I get that this is an addon, but if one method is better, then why not use that one for both?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.