While static routes can solve a large number of use-cases, an application also needs dynamic routes. Dynamic routes help us pass parameters as part of the route path. In this post, we will learn how to handle KoaJS Route Parameters with examples.

To understand how routes work in KoaJS, refer to the post on KoaJS Router.

However, if you are new to KoaJS, I will recommend you to start with post on getting started with Koa framework.

1 – Handling KoaJS Route Parameters

Let us create a simple example demonstrating route parameters in KoaJS.

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

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

router.get('/books/:id', (ctx, next) => {
  ctx.body = 'Book requested for id: ' + ctx.params.id
});

app.use(router.routes())

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

As you can see, we added :id in the path within the router.get() function. Basically, :id is a placeholder for a route parameter. Once we define the route parameter, we can retrieve the value for the parameter from the ctx.params object.

If we start the above application and visit http://localhost:3000/books/5, we get the output message as ‘Book requested for id: 5’.

We can also send anything other type of value in the id field (such as string) and even that will be available in ctx.params.

Lastly, it is also possible to have multiple route parameters in KoaJS.

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

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

router.get('/books/:id', (ctx, next) => {
  ctx.body = 'Book requested for id: ' + ctx.params.id
});

router.get('/books/:bookId/author/:authorId', (ctx, next) => {
  ctx.body = {bookId: ctx.params.bookId, authorId: ctx.params.authorId}
});

app.use(router.routes())

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

For example, here the second route contains two parameters – bookId and authorId. We can retrieve both from the ctx.params object.

2 – Pattern Matching with Route Parameters

We can also use regex to restrict URL parameter matching.

For example, if our requirement says that the bookId should only be 3 digits long number, we can use regex as follows:

router.get('/books/:id([0-9]{3})', (ctx, next) => {
  ctx.body = 'Book requested for id: ' + ctx.params.id
});

Note that this will only match if the id is 3 digits long. Anything less or more will result in HTTP 404 error code.

Conclusion

With this, we have successfully looked at KoaJS Route Parameters and how to handle them in our application.

Basically, route parameters make our application routes dynamic and flexible. Also, we can use multiple parameters in the route and also perform pattern matching using regex.

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 *