WEB FRAMEWORKS

Node.js and Koa.js - Hello world! Example

BCiriak • December 11, 2019

Requirements:

What is Koa.js?

Koa.js is web framework created and maintained by the team behind the another very popular Node.js web framework, called Express.js (check it out on the official Express.js website). In fact, Koa.js has many similarities (and many differences) to Express.js, and one could say, Koa is a modern version of Express. Koa.js uses newer version of JavaScript and its features. Visit the official Koa.js website for more information.

Why Koa.js?

There are couple of reasons to use Koa.js. First of all, as I already mentioned, it is developed by the team behind Express.js, which is the most popular Node.js framework out there. So the quality of the framework is very high. It is also very popular itself, with more than 25k stars on Github, it is up there with the most popular Node.js frameworks. Lastly, it is great for building web applications and APIs, it is modern, fast and very minimalistic framework.

"Koa.js is modern, fast and very minimalistic Node.js web framework, great for building web applications and APIs!"

Node.js and Koa.js

We need to have Node.js installed on our machine, that should be obvious. If we have Node and NPM, we are ready to initialise Node.js project like any other, with npm init or npm init -y if we want to save time and answer all the setup questions with yes. We can always change it later. So let’s setup our new Node.js app.

Node Project Initialisation

I have created new folder called koa-hello-world and this is where I am in my terminal. Let's begin. As I just mentioned, we start by running the npm terminal command npm init -y. This leaves us with basically empty package.json file, now we just need to create our entry point, and if you left the npm initialisation at default, it will be index.js. So let’s create the file and call it index.js. And we are ready to install and create Koa.js application.

Install Koa.js

Now, Koa is a JavaScript package like any other, we can find it on yarn website or npm website. So it can be installed with npm or yarn, with your common npm install koa. Give it a few moments and now Koa.js package should be included within our package.json as project dependency. Great stuff!

package.json
{
  "name": "koa-hello-world",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
  "dependencies": {
    "koa": "^2.7.0"
  }
}

Hello World! in Our Browser

All we have to do now, is to write the javascript code that will create Koa.js application, handle the request and start the server. All of this can be done by the following short JavaScript snippet copied from Koa.js website:

index.js
const Koa = require('koa');
const app = new Koa();

app.use(async ctx => {
  ctx.body = 'Hello World!';
});

app.listen(3000);

Start up the Server

At this point, we just need to start our server simply by executing the index.js file with node terminal command. We need to be within the directory that contains index.js file.

node index.js

If we head to localhost:3000 we should see “Hello World!" printed within our browser window. How simple and awesome! And that is all for this simple and short overview of the amazing Koa.js web framework.

Cow says bye!

Read More

You Don't Know JavaScript book Up and Running front cover.

JavaScript

You Don't Know JavaScript: Up and Going

First book in a series of books about JavaScript. It is an introduction to programming concepts and overview of JavaScript programming language.

What are web frameworks featured image

Web Frameworks

Web Frameworks, What are They?

Web frameworks are amazing tools. They help us build web and speed up our development. Many implement best practices of experienced teams of developers.

Single page application example

Web Frameworks

Single Page Application Example

What are Single Page Applications? In this article we will have look at Single Page Application example, understand how they work and what makes them SPAs.