Just like the request object, the response object is an equally important piece of an application. KoaJS Response Object is basically an abstraction on top of the vanilla NodeJS response object.

When working with KoaJS, we should use Koa’s response object only. In other words, we should not try and access the NodeJS response object directly.

Check out this post on KoaJS request object.

1 – KoaJS Response Object Demo

The KoaJS response object is embedded within the context object.

Let us see it in action.

const koa = require('koa');
const Router = require('koa-router');

const app = new koa();
const router = new Router();

router.get('/greeting', (ctx, next) => {
  ctx.body = "Request has been received"
  console.log(ctx.response);
});

app.use(router.routes())

app.listen(3000, function(){
   console.log('Server running on https://localhost:3000')
});

Here, we are basically logging the ctx.response object to the console.

Below is the console output of making the API call.

{
  status: 200,
  message: 'OK',
  header: [Object: null prototype] {
    'content-type': 'text/plain; charset=utf-8',
    'content-length': '25'
  },
  body: 'Request has been received'
}

As you can see, the status and message parameters are already set by Koa. The status code is 200 (OK) and the message is also OK.

If we don’t set the response body, KoaJS automatically sets the status code to 404. Once we set the response body, Koa sets the status to 200 by default.

2 – KoaJS Response Properties

KoaJS provides a bunch of useful properties as part of the response.

Let us look at them one by one.

  • response.header – This object contains all the response headers such as content-type, content-length and so on.
  • response.status – This object provides the response status code. Basically, response status code is the HTTP status code such as 200, 404 and so on. This is a very important field for the client application to determine how to treat the response.
  • response.message – This field contains the response message. In the example, Koa automatically sets it to OK. However, we can also use this field to set a custom message.
  • response.body – The body is probably the most important field. Usually, we access it using the context object (context.body). However, we can also access it via the response object. It can contain string values, objects, stream or buffer.
  • response.type – This field can set the content type of the current response.

Conclusion

With this, we have successfully looked at KoaJS response object and how to work with it in your application. Also, we have understood the various useful properties that are available as part of the response object.

If you are new to KoaJS, do check out the post on getting started with KoaJS.

Also, if you want to learn more KoaJS features, check out this post on setting up a redirect URL in KoaJS.

In case of any queries or comments about this post, please feel free to mention them in the comments section below.

Categories: BlogKoaJS

Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *