Server-Side Web Development with Koa JS

Next-generation web framework for Node JS

Shamoda Jayasekara
Tech It Out
4 min readApr 12, 2021

--

In this article, we will be looking at a minimalistic Node JS library called Koa JS. Koa is a library that very similar to Express, why because Koa is created by the same team who behind the Express. 😊 So, if you are familiar with Express you will see the syntax is very similar and you’ll pick it up very quickly.

Let’s create a simple RESTful API and see Koa in action.

First, let’s create a Node project by running the npm init command and it will create a package.json file in our project directory.

Now we can install Koa and other required node modules in our project by running the following command.

npm install koa nodemon koa-router koa-bodyparser

Okay, we need to create a file to implement our server. Let’s name it app.js and we need to make sure it resides in the root folder of our project. In this example, I’m writing all the business logic inside the app.js file, but it’s not a good practice. So, you can divide the application to separate modules after you got the basic concept of Koa. 😉

Next, we need to add start scripts to run our server. Add the following scripts to the package.json file.

"start": "node app.js",
"dev": "nodemon app.js"

Here we are using nodemon in our development environment. It will make things easier for continuous development. After following all the above steps your project directory and package.json file should look like this.

Cool, we have installed all the libraries and created the files we want for this project. Now, let’s create our server with basic CRUD operations. 😊

First of all, we need to import all the required modules to our app.js file. Just like this,

Here I’m using commonJS pattern to import node modules, you can also use ES6+ syntax to do the same thing. But if you use ES6+ syntax you have to use the babel library to transpile your code. Because most of the browsers cannot read ES6+ syntax.

Okay then, as our next step, we are going to set up the middleware of our application. Koa is heavily dependent on middleware. Therefore, it is necessary to set up our middleware properly inside the app.js file.

Next, we need to make our application to listen to the port number we have defined above.

Cool! Then we need a database. For this application, We are going to use a simple Array as our database. Inside that array, we are going to store 2 user objects called John and Ann. Just like this,

So far, we have imported the necessary modules, set up the middleware, and defined our data source. 😊

Now we have to define routes to perform CRUD operations. Let’s start with the GET method or reading data. So what we have to do here is, first define the route and then implement the method that needs to be executed whenever an HTTP request sent to that route. Look, 👇

Here, with ‘ctx’ we are referring to Koa context, remember it’s not a keyword and you are free to use any word you want. Okay, now we can move to the POST method or insert data into our database.

Then we have update and delete operations left to be implemented. These 2 operations are little bit tricky. Why, because first, we need to identify the object using the id, and then we need to perform the relevant operation on that object. Let’s start with the PUT method or update existing data in our database. 😊

Okay, so we have only one more method left to be implemented. Which is the DELETE method. So, let’s do it.

Cool! we have a fully functional CRUD application. Now it’s time to run the application and test our REST endpoints. We can run our application using the following command.

npm run dev

Let’s move to the Postman to test our end-points.

GET Request
POST Request
PUT Request
DELETE Request

In this article, we have implemented a simple RESTful API with Koa JS. It’s minimal but powerful. 😊 You can find the complete source code from here, 👇
https://github.com/shamoda/Koa-JS-Demo

I hope you have learned something from this article. Thanks for reading.
CHEERS!!!

--

--