REST API and Koa JS in simple words.

Uditha Ekanayake
4 min readMay 15, 2022

REST API

REST stands for Representational State Transfer. Roy Fielding presented the idea of REST in 2000. REST API is an architectural style of using HTTP requests to access data and resources.

REST API in simple words is an architecture of designing Application Programming Interfaces (API) to access data using a set of URLs.

Example: In a bookstore database we can use a REST API to get the list of books available. The URL would look like https://localhost:1234/books

This would return the set of books available from the database. We can use access a specific book using the book ID in the URL. In that scenario the URL would look like https://localhost:123/books/5

But what if we want to add a book to the database? Then the URL would be https://localhost:123/books with the book details in the body of the http request. But then it would be the same URL as getting all the available books.

We can use different HTTP methods to resolve this issue.

HTTP Methods

  • POST — Create
  • GET — Read
  • PUT — Update/Replace
  • DELETE — Delete

These are the most commonly used HTTP methods. We can use these methods for requests based on the need. In the above example we can use GET method with the URL https://localhost:1234/books to get all the books available in the database. We can use the same URL with the POST method to add a new book to the database. We can use the PUT method with URL https://localhost:123/books/5 and book details in the request body to update the details of the book with the ID as 5. We can use the same URL with DELETE method to delete the book with the ID as 5 from the database.

Constraints of REST API

There are some features that define the REST API and describes its architecture

  • Uniform interface — URIs refer to the resources and HTTP methods refer to the action to be performed on them
  • Stateless — Server does not store the state of the client
  • Cacheable — Rest services should be cacheable
  • Client-Server — Uses HTTP for client server communication
  • Layered System — Client and server is separated and the client does not see the complexities of the server side

Features of REST API

  • REST API is built on resources. A resource can be a book in the database or any kind of an item that can be identified by a noun.
  • Resources are identified using a Uniform Resource Identifier (URI). In the above example the book with the ID as 5 can be identified by https://localhost:123/books/5
  • There is a client and server communication. We can send request to the rest API to do a certain task such as edit the details of a book and it will return a response after processing our request.
  • Java Script Object Notation (JSON) or XML is commonly used to pass data between client and the server
  • REST API is lightweight and maintainable as it uses the principle of separation of concern
  • REST API is scalable in its design itself

Koa JS

Koa JS is a Node JS web application framework. It is designed to develop web applications and APIs. Koa JS is a minimalistic framework but features can be added using plugins. It has high performance, extensibility and reusability.

How to create a simple REST API using Koa JS

To begin with coding a new project folder can be created. Node JS should be installed to continue on this example. Following commands can be used to initiate the project with Node JS, install Koa JS, Koa JS Routes, Koa Body Parser and Cors respectively.

commands

We can create an app.js file which marks the entry point of the application.

app.js

APIs and Routers for that APIs can be added to separate files.

items.api.js

The router file for each API will handle the requests made by the client.

items.router.js

Finally we can modify the app.js file to include the defined Routes in the application and use the URL https://localhost:3000/items to access the data.

app.js

We can use a tool such as Postman to test the REST API. We can use other HTTP methods in the Router and define functions for them in the API for different functionalities according to the need.

Postman Results
Postman window

Koa JS is a good framework to create REST APIs in a simple and maintainable manner. It is minimalistic and easy to code.

--

--