KoaJS Request Object is an abstraction on top of Node’s vanilla request object. It helps us build additional functionality in our Koa applications.

We usually need to work with a request object when dealing with a HTTP endpoint. If you are not aware of how to create an endpoint in KoaJS, refer to this post on creating KoaJS route.

1 – KoaJS Request Object Example

The KoaJS request object is embedded within the context object. We can access the same via the ctx object.

Let us look at the below example:

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

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

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

app.use(router.routes())

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

As you can see, we log the request object within the route handler. The request object is available as ctx.request.

Below is the output that we receive when we hit the /greeting endpoint:

{
  method: 'GET',
  url: '/greeting',
  header: {
    host: 'localhost:3000',
    connection: 'keep-alive',
    'cache-control': 'max-age=0',
    'sec-ch-ua': '".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"macOS"',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
    accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'sec-fetch-site': 'none',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-user': '?1',
    'sec-fetch-dest': 'document',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8'
  }
}

As part of the request object, we get access to a set of several useful properties.

2 – Request Object Properties

Some of the important request object properties in KoaJS are as follows:

  • request.header – This provides access to all the request headers. Also, the request header itself is an object consisting of several additional properties.
  • request.method – Provides the request method (GET, POST, PUT or DELETE). In other words, these are the HTTP methods
  • request.url – This is the URL of the request endpoint. In our example, the value is /greeting.
  • request.query – In case our endpoint supports query parameters, the request.query object will be populated.
  • request.body – The body of the request. Basically, we use this for POST and PUT requests to send a payload of information.
  • request.params – This object contains the path parameters. Basically, if our routes are dynamic in nature, we will have some path parameters. Read more about KoaJS route parameters.

Conclusion

With this, we have successfully looked at the most important request object parameters in KoaJS. Basically, dealing with request object is important to build any application.

If you have any comments or queries 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 *