koajs / json

pretty-printed JSON response middleware
MIT License
198 stars 9 forks source link

[bug] string payload is not json encoded #24

Open damianobarbati opened 4 years ago

damianobarbati commented 4 years ago

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}"`;
});
jonathanong commented 4 years ago

why would you send a string JSON-encoded?

damianobarbati commented 1 year ago

@jonathanong for consistency: