Categories
Javascript

REST API with KoaJS and MongoDB (Part – 1)

We have previously written about REST API Development in length including tutorials using the Flask Framework and of course a multi part tutorial series using the Django REST Framework. In this tutorial series, we are going to focus more on JavaScript and see how we can build a very simple REST API using the KoaJS framework for NodeJS. We’re also going to use MongoDB and this no sql db list as our database.

Learning JavaScript

Before we can proceed, we should make sure that we’re proficient using JavaScript. We wouldn’t need any extra ordinary JS mastery but we need to have a certain level of command over JavaScript. If you are new to JavaScript and looking for some resources to learn the language, we have you covered. Please do checkout our JavaScript Learning Guide.

NodeJS and KoaJS

NodeJS has made JavaScript popular outside the browser world. There was a time, JS was meant to be used only on the browser side. For anything else, we would be using other languages like Java, Python, PHP etc. But then NodeJS came and proved that JavaScript is an equally viable language. With the very rapid growth of the platform, NodeJS today is being used for many types of programming and software development. From web backend and REST APIs to IoT tools and Desktop applications – NodeJS is literally everywhere.

There are many free and open source frameworks available for NodeJS but Express is perhaps the most popular option. Koa is quite like Express but it’s light weight, flexible and very simple. I personally am a big fan of this framework. At work, I have built multiple services on top of KoaJS and have been very happy with the outcome. This is why I chose Koa.

MongoDB

MongoDB has become very popular as a NoSQL database in the recent times. MongoDB is not like the traditional relational databases we’re so used to. Rather it’s a document oriented database where the documents look just like JSON. If you’re new to MongoDB, don’t worry, they have a brilliant University where you can take your courses free and master the database system. I have personally taken their courses and I would happily recommend the courses. They do cover some of the popular languages on the web. You will be learning to use MongoDB with Python / Java / NodeJS. Once you learn the concepts well, you should be able to transfer the knowledge to any other programming languages / platforms – so no worries if your favourite language is not available yet.

Getting Started

Before we can get started, make sure you have the latest NodeJS and npm installed. We would be using some of the modern JS syntax (ie. async / await), so please make sure you’re on the latest version of NodeJS (at the time of this writing, that would be Node 8). Please also make sure MongoDB is installed as well. On Windows you download the setup files and install them manually. On OS X, you can use a package manager like Homebrew. On Linux distros, you should be able to get them installed using the package manager available on your OS.

Creating The Project

Let’s first create a directory named “koa-example”.

Now we’re going to initialize the npm project.

The prompt will ask you a few questions. Once you complete answering them, we’ll be ready to start installing 3rd party dependencies.

Install KoaJS

Now we’re going to install KoaJS in our new project.

This should install KoaJS and save it as a project dependency in the package.json file.

A little more about Koa

Please note that Koa is being developed and maintained by the same people who work on Express. Koa is a much light weight version of Express except it follows a different path. Koa uses the modern JS features like async / await to make the code readable and maintainable. Although, in being light weight, Koa had to leave out many of the built in features in Express. Throughout our tutorial(s), we shall be installing third party packages, often to avail the same features which is prevalent in Express and other frameworks. Koa gives us a really minimal core and a very narrow, focused set of functionality. It lets us choose the components we need. That is in a way liberating and very flexible regardless of how inconvenient that might sound at the beginning.

One of the common Koa middleware I always install is koa-router which enables express like convenient routing on Koa.

Hello World!

Let’s start writing some codes, we shall of course start with the hello world example. We will first see the codes and then go through them.

The code is pretty straightforward. We are importing Koa and Router and creating new instances of them. We are then adding a new route / view to the router. Please note the usage of async functions. We mentioned earlier that Koa uses the latest JS features, notably the async and await features. If you are not familiar with the syntax, here’s a very in depth article that covers from the basics of promises, usages of generators and finally async and await. It would be a good read since Koa used to use generators to achieve similar results before async / await landed on Node.

The async function takes a parameter called ctx which is short for context. We can just set a body to the context and it will be rendered as response. If we pass certain types, for example a dictionary / object, it will be sent as JSON format. We can also access the request object using the context.

After the function is defined, we have used the routes middleware and launched the app on port 3000. If we browse the url http://localhost:3000 we should see the response we set.

Query Strings in KoaJS

Now that we have seen a basic hello world example, let’s see how we can access query strings in KoaJS. Query strings are appended to the url in the form – /hello?name=masnun – here the part starting from the question mark is the query string. We separate different parameters using & in the query string part. For example: name=masnun&country=bangladesh.

To access a parameter passed in the query string, we can use the ctx.request.query object to access the parsed querystring as JS object (dictionary). Let’s change our function to access the name from the query string and display a custom greeting.

Now visit – http://localhost:3000/?name=masnun and see how it works!

Accepting JSON

We have by now got used to the idea that we need to plug in 3rd party middlewares to achieve common tasks. One of the common thing in building REST APIs would be to accept JSON payloads. And like most other things, we need to use an external package to handle incoming JSON requests. In this case, we will use the koa-bodyparser package.

Now let’s modify the previous code to accept a POST request and parse incoming JSON so we can take the value for name variable from the JSON data. Here’s our modified code, first take a look and then we will discuss what we did different here:

The highlighted lines have been changed in this version. First we required the body parser package. Next we used it as a middleware. And finally, we accessed the value of name from the ctx.request.body object. Note, for query string it was request.query and now it’s request.body. The body parser parses the body of the request and sets the value there.

We can try our new code using the following request:

(The cURL command line was generated by Postman, feel free to use your favorite GUI tool for testing the APIs)

Logging Requests on Console

Our current app doesn’t list the requests it serves on the console. If we could get a nice log of what requests are being accepted and handled, it would help us with the workflow. We will be using the koa-logger package for just that.

Now we can use it in our app:

Now launch the app and make a few requests, you will notice nicely formatted output on your terminal.

Use Nodemon for Auto Reload

During development, it can be a tedious task to restart the app from time to time whenever you make some new changes. Nodemon is one of the tools which offer help with that. Install it globally and then just use nodemon . inside your current working directory. It will monitor file changes and restart the app as needed.

Learn more about Nodemon here – https://nodemon.io/ (along with installation instructions and use cases).

Up Next!

In this tutorial, we just got started using KoaJS and Koa Router package to build our views. In the next tutorial we shall resume our journey forward, learning to use MongoDB as our database.