DEV Community

Yonatan Bendahan
Yonatan Bendahan

Posted on

An Opinionated Node.js Boilerplate Using TypeScript & Fastify

After some time using Node.js for building backend APIs, I realised I have a ready-for-production boilerplate using my favorite build tools and libraries.


You can clone the boilerplate now using git from: https://github.com/yonathan06/fastify-typescript-boilerplate


Included in the boilerplate:

  • TypeScript (with module alias)
  • Development environment
  • Tests (using Jest)
  • Fastify
  • File based routing (using fastify-now)
  • Env vars config
  • CI with github actions
  • Docker image
  • Linting

TypeScript

Adding static types to your JavaScript code might look tedious, and it definitely feels like that when you just begin. But using TypeScript eventually saves a lot of “brain power”, especially when building large projects (try refactoring your models after using TypeScript and you’ll see what I’m talking about)
I used module alias in my typescript config (so imports from ‘…/lib/..’ will resolve to ‘@lib/..’). The problem was that TypeScript doesn’t switch back to regular module path when compiling, so had to create a little script to support that (resolve-module-alias.js) and call it after compilation.

Development

Using nodemon for watching file changes, that triggers TypeScript compiler and runs the server from /build

  • NOTICE: I am using TypeScript incremental builds, for fast recompiling. It can cause unexpected bugs, as I do not clear the build folder before each compilation. That means, if you remove a file from your src folder it might still present in the build folder. You will need to clear the build folder manually.

Fastify

In my humble opinion, Fastify is the best Node.js framework out there (yes, even better then Express). It comes with useful tools (schema validation, pino logger and more), and it is one of the fastest Node.js frameworks out there.

File Based Routing

A few months ago I built a Next.js app, with API serverless endpoints using Next.js API Routing.

After a while, the API backend got larger and larger, and I had to move to a standalone backend repo, to easily scale the backend API code(does that mean I used microservices? 😉)
The problem was, the Next.js API Routing uses a file based routing. For example /api/user.ts will resolve to my-domain.com/api/user endpoint.

I wanted to keep this feature when using Fastify, but didn’t find anything around (fastify-autoload was close to answer my needs, but wasn’t enough for me)

For that, I built a little plugin for Fastify, called fastify-now, that does just that: you pass it a folder path, and it loads the files there as routes (check the repo for documentation)

Env Vars

Using dotenv to load env files, and env-schema to validate them
I load different env files, depending on the NODE_ENV value. Meaning, if NODE_ENV=”production” it will try to load “production.env” from the root folder (the default is “development”).

Docker

A very simple docker file included, you can use it to build an image.

Notice that while building the docker image, it sets the NODE_ENV to “production”, so a production.env file must be present in the root folder before building the docker image.

Linting, Prettier, Testing & CI

Using Eslint and Prettier for a smooth development experience .

Using Jest with TypeScript for testing (suits for unit testing and e2e testing).

Even included a github action for running tests when pushing or PRing to master branch.

Database

I didn’t include any database connection and packages in this repo, feel free to add your own database


Hope this boilerplate repo will help you speed up your backend development process, without dropping important tools.


Looking for help and guidance with advanced web technologies? Feel free to contact me through my website: https://opsteacherpage.com/t/yonatanb

Top comments (0)