Categories
Koa Nodejs

Getting Request Data 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.

Getting the Content-Type Header of a Request

We can use the ctx.request.type property to get the Content-Type header of the request.

For instance, we can write the following code to display the value of that on the screen:

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

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

app.listen(3000);

Then if we send our request with the Content-Type header’s value set to application/json , then we’ll get that displayed on the screen when we get the response.

Get the Character Set of the Request

The ctx.request.charset property has the request character set.

Getting the Query String From a Request URL

The ctx.request.query property has query string parsed into a key-value pair.

Each query parameter of the query string is parsed into a key-value pair of an object.

For instance, we can use it as follows:

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

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

app.listen(3000);

In the code above, we just set the query property as the value of the ctx.body .

Then when we make a request to https://CraftyGrouchyCables–five-nine.repl.co?a=1&b=2, we get the following response:

{
    "a": "1",
    "b": "2"
}

As we can see, the key-value pairs from the query string are parsed as the key-value pairs of the object set as the value of ctx.request.query without doing anything ourselves.

The ctx.request.query property is also a setter, so we can set it to something else. However, it doesn’t support nested objects.

Check if the Request Cache is Fresh, or if its Content Has Changed or Not

We can check the request cache’s freshness by using the ctx.fresh property.

For instance, we can use it as follows:

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

app.use(async (ctx, next) => {
  ctx.status = 200;
  ctx.set('ETag', 'foo');
  if (ctx.fresh) {
    ctx.status = 304;
    console.log('fresh');
    return;
  }
  ctx.body = 'foo';
});

app.listen(3000);

In the code above, we check if the request cache is fresh by using the ctx.fresg property. If it’s fresh, then we’ll see the fresh string logged.

Otherwise, we won’t see it logged. If we make more than one request to our app, then we won’t see it logged since the cache has been populated with data that haven’t change we always return the same response.

There’s also a ctx.stale property to check that the opposite status of ctx.fresh is true .

Get the Protocol of Our Request

We can use the ctx.request.protocol property to get the request protocol. It’s either https or http .

If app.proxy is set to true , it can also check the X-Forwarded-Proto request header for the protocol.

Get the IP Address of the Device Making the Request

The ctx.request.ip and ctx.request.ips can get us the remote address of the device making the request.

The ips property gets us the IP address of all the devices that the request went through to make it to our app.

For instance, we can use the ip property as follows:

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

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

app.listen(3000);

Then when we make a request to our app, we get the IP address of the device that made the request.

We can also use the ips property if app.proxy is set to true . For instance, we can use it as follows:

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

app.proxy = true;

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

app.listen(3000);

Then we’ll see an array of IP addresses that the request went through to reach our Koa app.

Photo by Carles Rabada on Unsplash

Get the Request Subdomains

We can get the subdomains of the request URL by using the subdomain property.

For example, we can use that as follows:

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

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

app.listen(3000);

Then if our app lived in https://CraftyGrouchyCables–five-nine.repl.co, we get that the subdomains property will return [“craftygrouchycables — five-nine”] so we’ll get that displayed on the screen as the response value.

Conclusion

We can use the ctx.request property to get the IPs and the subdomains of the requested URL.

The protocol and query string are also available. Query strings are automatically parsed into an object.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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