Categories
Koa Nodejs

Receiving Requests with Koa

Spread the love

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

In this article, we’ll look at how to receive request data with Koa.

Receiving Headers

We can get the request headers of a request by using the ctx.request.header property.

For instance, we can get the headers as follows:

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

app.use(async (ctx, next) => {
  ctx.body = ctx.request.headers;
});

app.listen(3000);

In the code above, we just assigned the ctx.request.headers object as the value of ctx.body so that we’ll see the request headers as the response body.

Then we should see something like the following when we make a request to the / route:

{
    "host": "craftygrouchycables--five-nine.repl.co",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36 Edg/80.0.361.62",
    "content-length": "90",
    "accept": "*/*",
    "accept-encoding": "gzip, deflate, br",
    "cache-control": "no-cache",
    "content-type": "application/json",
    "postman-token": "fd561c8c-6419-4939-87f5-dc989d5b9845",
    "x-forwarded-for": "35.191.2.193",
    "x-replit-user-id": "",
    "x-replit-user-name": "",
    "x-replit-user-roles": ""
}

We can also set the request.header to set the request header.

Its alias is request.headers . They get and set the same data.

Getting the Request Method from a Request

We can use the ctx.request.method to get the request method of the request.

For instance, we can display it on the screen by writing the following code:

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

app.use(async (ctx, next) => {
  ctx.body = ctx.request.method;
});

app.listen(3000);

Like with the request.header property, we set the ctx.request.method as the value of ctx.body , then we see GET displayed on the screen if we make a GET request to the / route.

Get the Value of the Content-Length Request Header

We use the ctx.request.length to get the value of the Content-Length request header. If it’s present, it’ll return the value. Otherwise, undefined is returned.

Get the URL of the That’s Requested

The ctx.request.url property has the URL that’s requested by the HTTP request to our app.

We can display it as follows:

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

app.use(async (ctx, next) => {
  ctx.body = ctx.request.url;
});

app.listen(3000);

We just set the url property to the ctx.body to return it as the response body. If we make a request to the / route, then we get / displayed.

It can also be used as a setter. Therefore, it makes this property handy for doing URL rewrites. For instance, we can rewrite requests to /foo and rewrite them to / by writing the following code:

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

app.use(async (ctx, next) => {
  if (ctx.request.url.includes('/foo')){
    ctx.request.url = '/';
  }
  ctx.body = ctx.request.url;
});

app.listen(3000);

In the code above, we checked that if our request URL includes the /foo segment. If it does, then we change it to / .

Therefore, when we make a request to / or /foo , we’ll see / displayed on the screen.

Photo by Max Letek on Unsplash

Get the Original URL of the Request

We can use the ctx.request.originalUrl property to get the original request URL. It’ll be a relative URL. So if we make a request to /foo to a Koa app that has the following code:

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

app.use(async (ctx, next) => {
  ctx.body = ctx.request.originalUrl;
});

app.listen(3000);

We’ll see that /foo is displayed.

If we want to include the protocol and host of the server in the URL, then we can use the ctx.request.origin property instead.

For instance, we can write the following code:

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

app.use(async (ctx, next) => {
  ctx.body = ctx.request.origin
});

app.listen(3000);

Then we get the protocol and hostname of the server that our app resides in.

If we want to get the full URL, then we can use the ctx.request.href property.

For instance, we can write the following code:

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

app.use(async (ctx, next) => {
  ctx.body = ctx.request.href;
});

app.listen(3000);

Then when we make a request to https://CraftyGrouchyCables–five-nine.repl.co/foo, we get back the whole URL on the screen if we have the code above.

Conclusion

We can get various pieces of data from a URL with the request property of the ctx context object. It has the header, request URL, request method and more.

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.