DEV Community

ChrisLeboeuf
ChrisLeboeuf

Posted on

Getting started Koa in NodeJS

So you came here to learn a little bit about Koa? As a disclaimer, I should say I am no expert, but I have at least enough knowledge to get started with this lightweight framework. So the question is, what is Koa?

First of all, you heard right. It is a very lightweight framework. With only a few things right out of the box and about 550 lines of code, Koa was created to have the core middleware functionality for NodeJS and not much more. Koa was built by the same team of creators for Express. The big difference between Express and Koa is that Express comes with much more right out of the box. Simply put, I believe the best description I can give comes straight from the Koa website itself!

By leveraging async functions, Koa allows you to ditch callbacks and greatly increase error-handling. Koa does not bundle any middleware within its core, and it provides an elegant suite of methods that make writing servers fast and enjoyable.

Now one thing to note is that koa replaces the native NodeJS req and res objects and replaces them with a context 'ctx' object. The context object has request and response properties that are used instead. Koa is designed to improve the experience of writing middleware by using async/await. These heavily reduce the amount of code needed to write middleware functions.

Now that we know what Koa is. Let's get started with a simple hello world application!

First off I won't make any assumptions here. You will want to make a directory and do a quick npm init inside of that directory. Once you do that you need just a couple more things.

npm i koa koa-router koa-logger

Of course, in order to get started with Koa, we would install Koa. Isn't that crazy? Anyway along with that, we are installing koa-router for request routing, and koa-logger for development logging. So let's get a quick app going.

Create a file index.js and copy the following code. It is a slightly modified version of the standard Koa example that you will find in most tutorials.

const Koa = require('koa');
const Router = require('koa-router');
const Logger = require('koa-logger');

const app = new Koa();
const router = new Router();

// Response to GET requests
router.get('/', async (ctx) => {
  ctx.body = 'Hello, World!\n';
});

// Logging
app.use(Logger());

// Add routes and response to the OPTIONS requests
app.use(router.routes()).use(router.allowedMethods());

// Listening to the port
app.listen(8080, () => {
  console.log('Server running on port 8080');
});

Now we can start the server!

node index.js

The server should have been successfully created! Now open the browser and go to http://localhost:8080/. You should see the Hello, World! page.

When making requests to the server, you should see in your debug console something that looks like this.

Server running on port 8080
 <-- GET /
 --> GET / 200 8ms 14b
 <-- GET /
 --> GET / 200 5ms 14b
 <-- GET /
 --> GET / 200 4ms 14b

That would be the logger going off. Very useful if you want to ensure requests are going through.

Anyways, that will be all for now. I hope you enjoyed this simple Koa tutorial and learned something new here today! I hope to see you in the next blog. Happy coding hackers!

Top comments (0)