Categories
Koa Nodejs

Sending Response with Koa

Spread the love

Koa is a small framework that lets us create backend apps that run on the Node.hs platform.

In this article, we’ll look at how to send various kinds of responses with Koa.

Sending the Body

To send the response body, we can set the body attribute of ctx . For instance, we can send a response body as follows:

const Koa = require('koa');
const app = new Koa();

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

app.listen(3000);

In the code above, we set the ctx.body property to 'foo' . Therefore, that’s what we’ll get when we go to the / route with our browser or make a request to it with an HTTP client.

Sending Response Header

We can send responses in our Koa code by setting the ctx.response property.

To set the header, we can set the response header with the ctx.set method.

For instance, we can use it as follows:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.set('Access-Control-Allow-Origin', '*');
  ctx.set('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  ctx.set('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS');
  ctx.body = 'hello';
});

app.listen(3000);

In the code above, we called ctx.set to set various headers, including the Access-Control-Allow-Origin header, Access-Control-Allow-Headers header, and Access-Control-Allow-Methods header.

Once we make a request to / , we’ll see those headers in HTTP clients like Chrome or Postman.

Sending Response Status Code

We can send response status codes by setting the status code value to the responbse.status property.

For instance, we can do that as follows:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.status = 202;
  ctx.body = 'accepted';
});

app.listen(3000);

In the code above, we set the ctx.status ‘s value to 202. So when we make a request to the / route, we’ll get the 202 status code from the request.

Some common response codes that are send include:

  • 200 — OK
  • 201 — created
  • 202 — accepted
  • 203 — non-authoritative information
  • 204 — no content
  • 301 — move permanently
  • 302 — found
  • 303 — see other
  • 307 — temporary redirect
  • 308 — permanent redirect
  • 400 — bad request
  • 401 — unauthorized
  • 403 — forbidden
  • 404 — not found
  • 405 — method not allowed
  • 406 — not acceptable
  • 422 — unprocessable entity
  • 500 — internal server error
  • 501 — not implemented
  • 502 — bad gateway
  • 504 — gateway timeout

Sending Headers

We can set the ctx.response.lastModified property to the date value that we want.

For instance, we can set that as follows:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.response.lastModified = new Date(2020, 0, 1);
  ctx.body = 'foo';
});

app.listen(3000);

In the code above, we set the lastModified property to January 1, 2020, so when we make the request to the / route, we’ll get the Last-Modified from the response with the value of Wed, 01 Jan 2020 00:00:00 GMT .

We can set the Content-Type response header by writing setting the ctx.type property. For instance, we can do that as follows:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.type = 'text/plain; charset=utf-8';
  ctx.body = 'foo';
});

app.listen(3000);

In the code above, we have the following code:

ctx.type = 'text/plain; charset=utf-8';

to set the Content-Type header to ‘text/plain; charset=utf-8’ . Then we’ll see that as the value of the Content-Type header when we make the request to the / route.

To append one more header to the response, we can call the ctx.append to append more headers. It takes the key and value as its 2 arguments.

For instance, we can use it as follows:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.append('a', 1);
  ctx.body = 'foo';
});

app.listen(3000);

In the code above, we called the ctx.append method with the header key 'a' and the corresponding value of 1.

Then when we make a request to the / route, we’ll get the response header A with the value 1 appearing.

Conclusion

We can set response headers by calling the ctx.append method. To return a response body, we can set the ctx.body property with a value.

To set the status code of the response, we set the ctx.status property.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.