How to send JSON response using Koa

In Koa.js you don’t need to use some specific methods to make a JSON response.

For example, if you send a string:

the browser will show Content-Type equal to text/plain:

If you send an object or an array the browser will show Content-Type equal to application/json.

Examples:

Sending an array in Koa.js

Sending an object in Koa.js

Koa.js automatically converts ctx.body to JSON if it’s an object or an array.

If you liked this, please help spread the word by hitting the recommend button below. Thanks!

Tags:, ,