Build a simple web server with typescript and koa
By: Jean-Baptiste Terrazzoni
Tags: Backend, Web Development, Typescript, Koa, Nodejs

Photo by Kym Ellis on Unsplash

Build a simple web server with typescript and koa

Always wanted to create your own web servers ? Amazing, I was like you 5 years ago ! Today, I’ll do my best to show you, with simple examples, how to bootstrap a project handling simple HTTP requests.

First let’s start with why you should try typescript 🤔 ? Beside being the 3rd most loved language by developers, using a type safe language has some advantages:

It forces you to write better , safer code, least prone to runtime errors.

forces you to write , code, least prone to runtime errors. Collaborating with others is more convenient and faster.

is more convenient and faster. I even find developing with it to be easier and more efficient, due to the way IDEs are nicely integrated with it, with suggestions, linter checks, etc.

Finally, to structure the code, we’ll discover the framework Koa. With it, understanding the server flow is straightforward and the logic is clearly divided. It also integrates nicely with the latest javascript features (❤️ the native async support). And by almost doing nothing for you, it forces you to understand core concepts of NodeJS / backend development.

Let’s begin !

… with the basics. The following steps require nodejs and npm to be already installed.

Initiate your new repository with npm init . And after following the prompted instructions, you can install the needed packages.

As you can see, I installed some packages 2 times:

the package itself

itself and a @types/package version

The second will install the typescript interfaces. It automatically import type definitions of an existing package, so that every method and variable is already type checked.

Using node with typescript

Node doesn’t work natively with typescript. We’ll use a package that converts everything to javascript, at the runtime: ts-node .

Try that with an amazing Hello World! and call it server.ts .

Now, if you run it with node server.ts , the process will fail. We already installed the ts-node package before, but to use it, we have to setup a new package.json script.

Launch it with yarn start and … it works 😃

Finally, we can add a .gitignore file to avoid unwanted committed files.

Handle the first request — koa

Here comes the Koa framework. We’ll use it to structure our requests logic.

The flow is really easy to understand: the node process listens for incoming HTTP requests, treat it ,and returns a response. Koa transpose it in a beautiful visual way. For example, the following snippet of a GET / request returns the message Hello World! :

Koa only handles the part of receiving the incoming request and returns the response you’re giving it. If you want to handle anything else, like a body, routes, etc …, you have to set it up yourself.

That what we just did here, with the koa-router package returning the Hello World message only on the home route / .

Other frameworks like express would have everything packed for you in a single package. But here at least, you will see and understand core concepts of the web 😉

Finally, we can talk about the method app.use() . It allows you to chain custom methods for each route, or on higher levels. Each method modifies a context object that is passed until the last method. We call those middlewares :

The last method is called the controller (see the helloWorldController from before).

When the method end, the HTTP response is sent !

Amazing ! Now, you should be able to handle basic route requests on Node. We will see later how to structure and configure of your app.

You also want to see how this works in a larger scale project ? Definitively check my ongoing open market project ! ❤️