Step-by-step: Building a Node.js server (2021 edition) — Part 1/4

Koa.js, GraphQL, MongoDB, Docker, Mocha, Typescript
  • With Koa.js we will implement our main logic. It is the core of our server and it will tie all the components together.
  • MongoDB will allow us to manage a database to store and retrieve information.
  • GraphQL will allow us to implement an API to interact with this database. Coupling GraphQL with Typescript will allow us to use GraphQL Code Generator to have a single source of truth for our GraphQL, Typescript and MongoDB models. 🤯
  • Using Mocha, we will ensure our server behaves correctly by testing both our GraphQL endpoints and our MongoDB CRUD methods (database accessors).
  • Docker will facilitate the deployments and the installation of all the tools for each developer of the team, without asking them to install a bunch of software on their machine.
  • Everything has to be written in Typescript, as it is more comfortable to work with strongly typed Javascript, and cheaper and faster to detect potential errors at compile-time rather than at runtime.
  • We will also cover some Quality Of Life (QOL) tooling: this means using tools like eslint, git hooks and nodemon. It will speed up the iterations when writing code.

Getting started: a Typescript-ready project

npm i -D typescript @types/node

🐨 Koa.js

npm i koa koa-router @types/koa-router

Some Quality of Life (QoL) tooling

📃 Enforce the code convention

npm i -D eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser
  • We are using the code convention of the extensions listed under extends. This is recommended for all typescript projects, and there are many other extensions available.
  • We can add as many custom rules as we want. For instance, I like making sure all the console.log() calls are removed in the final code, to avoid the typical console.log("debug") calls (or worse 😉).
Unexpected console statement, line 10 of src/server.ts
Unexpected console statement.
// eslint-disable-next-line no-console
"lint": "eslint . --fix"

Avoid pushing ill-formatted code to the remote

🔄 Set up hot reload

npm i -D nodemon ts-node
"start": "nodemon"



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store