Caching is one of the most important aspects of building a high-performance application. Most web frameworks provide caching capabilities. The same is the case with KoaJS. In this post, we will learn how to setup KoaJS Cache Middleware using the koa-static-cache package.

Why do we actually need caching?

Caching basically allows us to store reusable responses to make future responses faster. By default, every browser has an implementation of the standard HTTP cache. To leverage it, the backend server needs to provide correct HTTP header directives. Using these directives, the browser can determine when and how long to cache a particular resource.

If you are new to KoaJS in general, I will recommend you to start with the post on getting started with KoaJS.

1 – Benefits of Caching

There are several benefits to caching in general.

  • Caching decreases network costs. If a particular piece of content is request many times and it is cached, we can save a lot of bandwidth by not having to send it over the wire again and again.
  • Caching enables the content to be available even when the server is offline.
  • Caching also improves the overall performance and speed of our web application.

In short, if done correctly, caching is an extremely advantageous feature that every application should try and leverage.

2 – Installing the koa-static-cache package

In order to implement caching in KoaJS, we need to install the koa-static-cache package for our project.

Execute the below command.

$ npm install --save koa-static-cache

Once the package installation in successful, we can proceed to use it in our application code.

Also, for demo purpose, we will create a folder named public in the root directory of our project. Within this folder, we will create a simple text file named demo.txt. You can put any text message in this file.

3 – Using the KoaJS Cache Middleware

Using the KoaJS caching middleware is actually quite simple.

See the below example:

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

const path = require('path');
const staticCache = require('koa-static-cache');

app.use(staticCache(path.join(__dirname, 'public'), {
  maxAge: 24 * 60 * 60  
}))

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

Let us understand what is going on here.

After importing koa and creating an application instance, we also import the koa-static-cache. Then, we configure the koa-static-cache package by calling the app.use() function.

Basically, the koa-static-cache is a middleware to cache server responses on the client side. The cache-control header is set according to the options we provide while initializing the cache object.

For example, in the above snippet, we provide the maxAge value to support caching for 1 day. We can change this value depending on the properties of the file. Also, we specify that we want to cache all the files within the public folder.

If we run the application now and hit http://localhost:3000/demo.txt, the file will be returned with an ETAG value. The next time when we request the same file again, the browser will send the ETAG value as part of the request header. If the ETAG value has not changed, the client will be told that you still have the correct copy and should use the same copy.

Apart from maxAge, there are also several other options available. You can read about them at this link.

Conclusion

There is no doubt that caching is a fundamental requirement for building high-performance applications these days. KoaJS provides caching support by means of a middleware function we can configure based on our requirement.

Another important feature for modern applications is error handling. Check out this post about how to perform error handling in KoaJS.

Want to create a REST API in KoaJS? Check out this post on creating a KoaJS REST API.

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 *