Published in


How to Build a REST API with KoaJS

How to Use Koa.js to write server-side code that uses node.js as the runtime engine.

What is Koa?

Koa is designed and built by the team behind ExpressJS with additions such as promises and async/await support in its core. These ES2015 features are used to tackle API's asynchronous calls. Distributed as a lightweight Node.js framework, Koa provides a minimal interface for developing web applications and APIs.

Features of Koa

Some of the features of the Koa framework include:

  • Support for ECMAScript 6 (/ES2015) by default
  • Developers can use generators as functions to stop and resume the code execution
  • Simplifies the use of Error handling by using middleware more effectively
  • Identifies and understands all HTTP methods
  • Even before Express, Koa had support for async/await

Getting Started

To start using Koa as a server-side framework you will have to first install it. Create a directory where you will want to place all the project related files. We will first initialize it as a Node.js project.

Our First Koa App

To understand Koa better, and point out the differences with a commonly used framework such as ExpressJS, we are going to first write an obligatory Hello World program.

Wait, what is ctx?

Another important feature that we use in our bare minimum example is ctx. I do hope you noticed it there.

Side Tip: Installing nodemon

Before I start on REST APIs, the core of the article, I want to introduce a great tip that is helpful in building a Node.js application. During the development mode, irrespective of the framework I am using, I use nodemon as a dependency to watch for file changes and automatically restart the application. This eliminates the need of running node [filename].js command again and again. You can totally, skip this step and move on the next one where I show the steps for writing the REST API.

Building the REST API

Finally, you have arrived at the point where you can start building the REST API. You have to install dependencies for the API to work. Let’s install them.

What are these dependencies for?

koa-body is a body-parser middleware. It supports urlencoded, multi-part and json request bodies. Basically, it helps to create and respond to HTTP POST requests which are available as a form field, or a file upload, etc. It tells the server that the incoming request from the client has a body of data. ExpressJS uses the same approach in handling body requests.

Creating a route to handle `GET` request

Below is the code for handling a GET request in Koa. Add the following code to books.js.

Congratulations! 🎉 You just build your first route using Koa.

Next step is to create a route to fetch a book by its id. It is going to use the similar approach as the previous route, plus we see how to extract information from request.params object of an incoming request.

Handling a POST request

The last route you are going to build for this demonstration is going to handle POST requests.

You can find the complete code used in this tutorial at this Github repository 👇

Building A Web Or Mobile App?

Crowdbotics is the fastest way to build, launch and scale an application.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aman Mittal

👨‍💻Developer 👉 Nodejs, Reactjs, ReactNative | Tech Blogger with 2M+ views at Medium| My weekly dev newsletter 👉