DEV Community

Georgi Marokov
Georgi Marokov

Posted on • Edited on • Originally published at worldwildweb.dev

4 2

Node.js Restful API template with TypeScript, Fastify and MongoDB

Why

Have you recently started a new Node.js API project? Did you use some template or started the project from scratch?
I was asking the same questions myself and I was looking for minimal boilerplate for a while. There were so many options that it was hard to pick one.
Most of them are using Express.js, others are using ES5 or lack test setup.
So I decided to spin one on my own and reuse it in the future. Here is the repo at GitHub.

How

My setup has the following characteristics:

API

  • Node version 10 or later
  • TypeScript for obvious reasons
  • Fastify for its asynchronous nature and being faster than Express or Restify
  • Nodemon in development for watching for changes and restart the server

Data

  • MongoDB with Mongoose
  • Docker for MongoDB service

Tests

  • Jest for being the de facto in Node testing
  • In memory Mongod server for easily mock the DB
  • Coverall for coverage collector after Jest report is generated

Code formatting and static analysis

  • ESLint config
  • Prettier config attached to the linter
  • Editor config

Documentation

  • Swagger UI for API documentation
  • Postman collections attached from testing the endpoints

CI

  • Continuous integration in Travis CI. Steps:
  1. Install dependencies
  2. Run tests
  3. Collect coverage and pass it to Coverall

And thats it! I hope it's minimal enough.
Please share some ideas for improvement. I thought of API versioning but Fastify seems to support that out of the box.
API key authentication was also something I was considering, but there were so many available options of implementations. If you have something in mind would love to discuss it in the comments.
Happy coding!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay